开发者

how can i disable / enable all html buttons on a page using jquery

i have a lot of ajax calls and i want to make sure noone clicks any button during the ajax calls. So i want to disable all buttons right before any ajax call and then reenable them after the return of the ajax call. I know i can disable a single button like this:

 $("#buttonId").attr('disabled', 'disabled');
 $("#buttonId").removeAttr('disabled');

but what is t开发者_开发百科he line in jquery to disable / reenable all buttons. I DONT want to use select by class as i then have to remember to put a certain class name on all buttons.

NOTE: they are all NOT submit buttons. Some are used for popping up dialogs, etc so

<input type="button" name="button" value="Show Popup" />


Your first task is to define what a button means. Is it a submit buttons that you want to disable? If yes then you could use the :submit selector if those are submit buttons:

$(':submit').attr('disabled', 'disabled');
...

If they are not all submit buttons then you could try a selector by a tag type:

$(':submit, :button').attr('disabled', 'disabled');

And if you also can have a mix of tags that can be used as AJAX calls and normal calls you need to use a class selector to disambiguate them.


Did you try $(":button") ? That would only do literal buttons though. "input would be more broad, but would also match radio buttons.

Also, note that in your example, jQuery has to do the work of finding the button (or all buttons twice). Chaining the function calls would avoid that problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜