jQuery jQuery禁用css类的所有按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3769223/
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
jQuery disable all button of a css class
提问by michele
I have n buttons. This button has the same name: setAlg.
我有n个按钮。此按钮具有相同的名称:setAlg。
I would disable all the buttons that has this name.
我会禁用所有具有此名称的按钮。
I have tried
我试过了
$("input[name='setAlg']").attr("disabled",true);
but it doesn't work.
但它不起作用。
How I can do?
我能怎么办?
Thanks a lot.
非常感谢。
回答by Sarfraz
Make sure to wrap your code in ready handler:
确保将您的代码包装在就绪处理程序中:
$(function(){
$("input[name='setAlg']").attr("disabled", true);
});
But If you are using the same class for those input fields (as per your question title), you need to try this instead:
但是,如果您对这些输入字段使用相同的类(根据您的问题标题),则需要尝试以下操作:
$(function(){
$("input.myclass").attr("disabled", true);
});
Where myclass
is supposed to be the class name used for those input fields.
Wheremyclass
应该是用于这些输入字段的类名。
回答by mkoistinen
Chris Pebbles suggestion should work fine, but I am confused. In your question's title, you specificall state "jQuery disable all button of a css class", however, you then provide an example which shows that you really want to disable all buttons that have a nameattribute of setAlg
.
Chris Pebbles 的建议应该没问题,但我很困惑。在您的问题标题中,您具体说明了“jQuery 禁用css 类的所有按钮”,但是,您随后提供了一个示例,表明您确实想要禁用所有具有name属性的按钮setAlg
。
Two points come to mind. First, your buttons, since they all have the same name may produce unexpected results for you when the user submits your form (if this is what they do, but I may be wrong here). If this is the case, the you probably really do want to set all of them to the same class, not name.
想到两点。首先,您的按钮,因为它们都具有相同的名称,当用户提交您的表单时,它们可能会为您产生意想不到的结果(如果这是他们所做的,但我可能在这里错了)。如果是这种情况,您可能真的希望将它们全部设置为同一个class,而不是name。
<input class="setAlg">...
Second point is that if you do want to make your jQuery to work with a class, you'll need to do:
第二点是,如果您确实想让 jQuery 与类一起工作,则需要执行以下操作:
$("input.setAlg").attr("disabled","disabled");
I hope this was helpful.
我希望这可以帮到你。
回答by Shervin Asgari
I think you forgot quotes
我想你忘记了报价
$("input[name='setAlg']").attr('disabled','true');
If this doesn't work then maybe your selection doesn't work
如果这不起作用,那么您的选择可能不起作用
回答by Sampath
You can disable a button by using below mentioned steps.
您可以使用以下提到的步骤禁用按钮。
Say your button class name = setAlg
说你的按钮类名 = setAlg
Step 1:
第1步:
Using Jquery attr Attribute :
使用 Jquery attr 属性:
$('.setAlg').attr('disabled', 'disabled');
Step 2:
第2步:
Using Jquery addClass CSS Class :
使用 Jquery addClass CSS 类:
$('.setAlg').addClass('disabled');
Step 3 :
第 3 步:
Then by Declaring CSS Style Class :
然后通过声明 CSS 样式类:
<style type="text/css">
.disabled
{
background: none repeat scroll 0 0 burlyWood;
cursor: default !important;
}
</style>
I have written a blog post about this check How to Disable a Button Using Jquery and CSS ?
我写了一篇关于此检查的博客文章如何使用 Jquery 和 CSS 禁用按钮?