jQuery禁用按钮不起作用 [英] jQuery to disable button not work

查看:128
本文介绍了jQuery禁用按钮不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾经将事件绑定到我的按钮,如:

  $(input [name = add])。live ( 点击,函数(){...}); 

但是我有另一个功能让它们启用和禁用,如:



$ $ p $ $ $ c $ RefreshBox(){

$(input [name = add])。each(function(){
if(...)
$(this).attr(disabled,disabled);
else
$(this).attr(disabled, );
})

}

事实并非如我所料。



PS:
jQuery 1.4.1
浏览器:IE8,禁用按钮后仍然是静止的能行得通。
浏览器:Chrome浏览器,禁用按钮成功。 您需要使用 prop 函数而不是 attr

  $(this).prop(disabled,true); 

  $(this).prop(disabled,false); 

编辑

OP使用jQuery pre 1.6.1,这就是为什么 attr 对于 disabled 不能正常工作的原因对于较老的IE)。虽然 prop 对于被禁用是首选,1.6.1 attr 应该(并且确实)工作。

I used to bind event to my button like :

$("input[name=add]").live("click",function(){...});

But I got another function to make them "enable" and "disabled" like :

RefreshBox() {

    $("input[name=add]").each(function(){
        if(...)
            $(this).attr("disabled","disabled");
        else
            $(this).attr("disabled","");
    })

}

But in fact that it doesn't work as I expected. Buttons are still can be click and the event still works smoothly.

PS: jQuery 1.4.1 Browser: IE8 , after button disabled still can work. Browser: Chrome, disabled button success.

解决方案

You'll want to use the prop function instead of attr:

$(this).prop("disabled", true);

and

$(this).prop("disabled", false);

EDIT

OP was using jQuery pre 1.6.1, which was why attr for disabled wasn't working quite right (for older IE). While prop is preferred for disabled, post 1.6.1 attr should (and does) work as well.

这篇关于jQuery禁用按钮不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆