Javascript 禁用页面上的所有按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30479448/
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
Disable all buttons on page
提问by Fred
I have an MVC view with a number of buttons on it (each item in a basket renders 2 buttons)...
我有一个 MVC 视图,上面有许多按钮(篮子中的每个项目呈现 2 个按钮)...
<button class="pbbg" id="ProductMinus_161637" type="button">-</button>
<button class="pbbg" id="ProductPlus_161637" type="button">+</button>
(they both have an onclickevent)
(他们都有一个onclick活动)
When either of these buttons are pressed I want to disable all the buttons for every product until the basket has finished updating.
当按下这些按钮中的任何一个时,我想禁用每个产品的所有按钮,直到篮子完成更新。
The click event calls a JavaScript function which in turn makes an Ajax call. Following this postthe first thing I try to do is disable all the buttons.....
click 事件调用一个 JavaScript 函数,该函数又进行一次 Ajax 调用。在这篇文章之后,我尝试做的第一件事就是禁用所有按钮.....
$("input[type=button]").attr("disabled", "disabled");
Then after the Ajax call returns reenable them....
然后在 Ajax 调用返回后重新启用它们....
$("input[type=button]").removeAttr("disabled");
I am getting no errors but the buttons are not disabled.
我没有收到任何错误,但按钮没有被禁用。
Where I am going wrong?
我哪里出错了?
回答by Tushar
Your selector is wrong. instead of input..selector you should use :buttonpseudo-selector.
你的选择器是错误的。input..你应该使用:button伪选择器而不是选择器。
You can use :buttonselector to select all the buttons.
您可以使用:button选择器来选择所有按钮。
$(':button').prop('disabled', true); // Disable all the buttons
To enable all the buttons:
要启用所有按钮:
$(':button').prop('disabled', false); // Enable all the button
EDIT
编辑
If you want to disable only the buttons whose id starts with Productuse:
如果您只想禁用 id 以Productuse开头的按钮:
$('button[id^="Product"]')
回答by jered
Probably because you're not using the inputtag, you're using the straight buttontag. Try $("button").attr("disabled", "disable")instead.
可能是因为您没有使用input标签,所以您使用的是直接button标签。试试吧$("button").attr("disabled", "disable")。
回答by Zee
You need to use buttonselector. Try proplike this
您需要使用button选择器。prop像这样尝试
$('button').prop('disabled', true);

