Jquery BlockUI 插件 - 阻止输入字段

发布于 2024-12-28 07:27:44 字数 505 浏览 2 评论 0原文

我想知道是否可以使用 JQuery BlockUI 插件直接禁用输入字段。

我看到 jquery 插件上的示例。

http://jquery.malsup.com/block/#element

当我在 jquery 选择器中只给出输入字段 id 时,它不起作用。

$(document).ready(function() { 

    $('#blockButton').click(function() { 
        $('#inputId').block({ message: null }); 
    })

当我只给出输入字段 id 时,它不起作用,但是如果我给出锚标记 id 或 div 标记 id,它就可以正常工作。

有没有一种解决方案可以仅阻止输入字段(文本、选择等)。

请告诉我。

I would like to know if JQuery BlockUI plugin can be used to disable a input field directly.

I see the samples on jquery plugin.

http://jquery.malsup.com/block/#element.

when I give just the input field id in the jquery selector, it is not working.

$(document).ready(function() { 

    $('#blockButton').click(function() { 
        $('#inputId').block({ message: null }); 
    })

when I just give the input field id, it doesn't work, but instead if i give anchor tag id or a div tag id it is working fine.

is there a solution to block just the input fields(text, select etc).

please do let me know.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

童话 2025-01-04 07:27:44

我编写了一对包装函数来调用 BlockUI 的 block() 函数。我写它们有两个原因:1.设置一些默认选项,2.防止元素被多次阻塞。

我遇到了和你一样的问题,IE 在尝试阻止 INPUT 元素时抛出异常。我修改了我的块函数来检查被阻止的元素是否是 INPUT,如果是,它会禁用 INPUT 并阻止 INPUT 的父元素。您需要将输入包装在 DIV 内(充当父级),以便仅阻止 UI 的一小部分。

// Wrapper function for the BlockUI Plugin http://jquery.malsup.com/block/
// This function blocks an element.
// @param {String, Object} element - Reference to the element to block, either the selector string, or the jQuery object itself
// @param {Object} options - a hash of options to pass to the block() call
function blockElement(element, options) {

    if (typeof element == 'string') {
        element = $(element);
    }

    if (element.length > 0) {
        if (typeof options == 'undefined') {
            options = {};
        }        

        initHash(options,
            { message: 'Please Wait',
                css: { width: '20%', padding: '3px' },
                overlayCSS: {},
                cursor: 'wait'
            }
        );

        initHash(options.css, { cursor: options.cursor });
        initHash(options.overlayCSS, { cursor: options.cursor });

        var blockOptions = {
            message: options.message,
            css: options.css,
            overlayCSS: options.overlayCSS,
            fadeIn: 0
        }

        if (!$.support.leadingWhitespace) {
            // disable fading in IE browsers less than IE9, it's slow to animate
            blockOptions.fadeIn = 0;
        }

        // if an element is already blocked, do not try to block it again
        var isBlocked = element.attr('data-isBlocked');
        if (isBlocked != 'true') {
            element.attr('data-isBlocked', true);
            // do not try to block input elements, it breaks in IE
            // instead disable the input and block it's parent
            if (element.is('input')) {
                element.parent().block(options);
                element.attr('disabled', 'disabled');
            } else {
                element.block(blockOptions);
            }

        }
    }
}

// Unblocks an element that was blocked using blockElement()
// @param {String, Object} element - Reference to the element to block, either the selector string, or the jQuery object itself
function unblockElement(element) {
    if (typeof element == 'string') {
        element = $(element);
    }

    var options = {};
    if (!$.support.leadingWhitespace) {
        // disable fading in IE browsers less than IE9
        options.fadeOut = 0;
    }

    if (element.length > 0) {
        element.attr('data-isBlocked', false);
        if (element.is('input')) {
            element.removeAttr('disabled');
            element.parent().unblock(options);
        } else {
            element.unblock(options);
        }
    }
}


// initalize a hash/map/associative-array with default values
// if the keys already exist, then they are left alone
// @param: {Object} targetHash - the hash that is going to be initalized
// @param: {Object} defaults - a hash containing the default values that should be added to targetHash
// @usage: initHash(targetHash, {a:1,b:2,c:3});
function initHash(targetHash, defaults) {
    $.each(defaults, function (index, value) {
        if (!(index in targetHash)) {
            targetHash[index] = value;
        } else {
            if (targetHash[index] == null || targetHash[index] == undefined) {
                targetHash[index] = value;
            }
        }

    });
}

I wrote a pair of wrapper functions to make calls to BlockUI's block() function. I wrote them for two reasons: 1. To set up some default options, 2. To prevent elements from being blocked more than once.

I ran across this same issue as you, where IE throws an exception when trying to block an INPUT element. I modified my block function to check to see if the element being blocked is an INPUT, and if so, it disables the INPUT and blocks the INPUT's parent instead. You'll want to wrap the input inside a DIV (to act as the parent) so that only a small portion of your UI is blocked.

// Wrapper function for the BlockUI Plugin http://jquery.malsup.com/block/
// This function blocks an element.
// @param {String, Object} element - Reference to the element to block, either the selector string, or the jQuery object itself
// @param {Object} options - a hash of options to pass to the block() call
function blockElement(element, options) {

    if (typeof element == 'string') {
        element = $(element);
    }

    if (element.length > 0) {
        if (typeof options == 'undefined') {
            options = {};
        }        

        initHash(options,
            { message: 'Please Wait',
                css: { width: '20%', padding: '3px' },
                overlayCSS: {},
                cursor: 'wait'
            }
        );

        initHash(options.css, { cursor: options.cursor });
        initHash(options.overlayCSS, { cursor: options.cursor });

        var blockOptions = {
            message: options.message,
            css: options.css,
            overlayCSS: options.overlayCSS,
            fadeIn: 0
        }

        if (!$.support.leadingWhitespace) {
            // disable fading in IE browsers less than IE9, it's slow to animate
            blockOptions.fadeIn = 0;
        }

        // if an element is already blocked, do not try to block it again
        var isBlocked = element.attr('data-isBlocked');
        if (isBlocked != 'true') {
            element.attr('data-isBlocked', true);
            // do not try to block input elements, it breaks in IE
            // instead disable the input and block it's parent
            if (element.is('input')) {
                element.parent().block(options);
                element.attr('disabled', 'disabled');
            } else {
                element.block(blockOptions);
            }

        }
    }
}

// Unblocks an element that was blocked using blockElement()
// @param {String, Object} element - Reference to the element to block, either the selector string, or the jQuery object itself
function unblockElement(element) {
    if (typeof element == 'string') {
        element = $(element);
    }

    var options = {};
    if (!$.support.leadingWhitespace) {
        // disable fading in IE browsers less than IE9
        options.fadeOut = 0;
    }

    if (element.length > 0) {
        element.attr('data-isBlocked', false);
        if (element.is('input')) {
            element.removeAttr('disabled');
            element.parent().unblock(options);
        } else {
            element.unblock(options);
        }
    }
}


// initalize a hash/map/associative-array with default values
// if the keys already exist, then they are left alone
// @param: {Object} targetHash - the hash that is going to be initalized
// @param: {Object} defaults - a hash containing the default values that should be added to targetHash
// @usage: initHash(targetHash, {a:1,b:2,c:3});
function initHash(targetHash, defaults) {
    $.each(defaults, function (index, value) {
        if (!(index in targetHash)) {
            targetHash[index] = value;
        } else {
            if (targetHash[index] == null || targetHash[index] == undefined) {
                targetHash[index] = value;
            }
        }

    });
}
山有枢 2025-01-04 07:27:44

您可以将输入设置为只读,而不是尝试阻止它:

$('#inputId').attr('readonly', 'readonly');

或者

$('#inputId').prop('readonly', true);

You could just set the input to readonly instead of trying to block it:

$('#inputId').attr('readonly', 'readonly');

or

$('#inputId').prop('readonly', true);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文