使用 jQuery 禁用控制

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5816832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 19:51:30  来源:igfitidea点击:

disable control using jQuery

jquery

提问by munish

I want to disable control on click for this i have added attr to the control 'disabled' and it is working fine in IE but not in Firefox. The code i have written is

我想为此禁用单击控制,我已将 attr 添加到控件“已禁用”中,它在 IE 中运行良好,但在 Firefox 中运行良好。我写的代码是

$(obj).attr('disabled','disabled');

If I am missing something then please give me idea.

如果我遗漏了什么,请给我想法。

回答by Alex R.

$(obj).attr('disabled', true);

回答by Chinmayee G

Try this

尝试这个

$(obj).attr("disabled","disable")

note value of attribute "disabled" is "disable" not "disabl*ed*"

注意属性“禁用”的值是“禁用”而不是“禁用* ed*”

回答by CarneyCode

This is the code I use to disable or re-enable a control:

这是我用来禁用或重新启用控件的代码:

function handleControlDisplay(className, foundCount, checked) {



    var button = jQuery(className);



    if (foundCount > 0 && foundCount == checked) {

        // enable

        button.removeAttr("disabled");

    }

    else {

        // set the disabled attribute

        button.attr("disabled", "true");

    };

}

回答by grahamesd

The very complete answer is here: Disable/enable an input with jQuery?

非常完整的答案在这里:Disable/enable an input with jQuery?

TL;DR use the .prop() method

TL;DR 使用 .prop() 方法

$("input").prop('disabled', true);
$("input").prop('disabled', false);

回答by Fabian Madurai

Here's an example of disabling a button after it has been clicked. Notice that you pass the "this" object to the click event handler :

这是在单击后禁用按钮的示例。请注意,您将“this”对象传递给 click 事件处理程序:

<input type="button" value="Submit" onclick="disable(this)"/>

And the javascript:

和 javascript:

    <script>
     function disable(control){
       control.disabled = true;
      }
   </script>