removeAttr()问题

举起手来 – 我无法弄清楚它有什么问题。 这是一个错误还是错误的代码?

$(document).ready(function() { $("#rem_but").click(function(){ var mail_name = $("#mail_rem").val(); var dataString = 'mail_name='+ mail_name; if (mail_name.val() == "") { $("#rem_but").attr("disabled",true); } else { $("#rem_but").removeAttr("disabled"); }; }); }); 

因此,当没有输入时,按钮会正确返回false – 当字段中有输入时 – 仍然按钮返回false,因此removeAttr()不起作用 – 为什么? 问候。

你在使用jQuery 1.6.x吗?

如果是这样,那么你应该尝试使用.prop()函数。 见下文:

使用jQuery禁用/启用输入?

此外,在您的if语句中无需继续选择$("#rem_but") 。 根据您的代码,我建议$(this)代替 –

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

这应该工作 –

 $(document).ready(function() { $("#rem_but").click(function(e) { e.preventDefault(); var mail_name = $.trim($("#mail_rem").val()); var dataString = 'mail_name='+ mail_name; if (mail_name === "") { $(this).prop("disabled", true); } else { $(this).prop("disabled", false); } }); }); 

这是工作的jsFiddle代码 –

http://jsfiddle.net/4rPc5/

更新的代码 –

http://jsfiddle.net/4rPc5/2/

try (mail_name.val() == "")更改为(mail_name == "")

也许你需要将disabled属性设置为’false’?

 if (mail_name.val() == "") { $("#rem_but").attr("disabled",true); } else { $("#rem_but").attr("disabled",false); }; } 

或者将其设置为空字符串

 if (mail_name.val() == "") { $("#rem_but").attr("disabled",true); } else { $("#rem_but").attr("disabled",""); }; }