removeAttr()问题 [英] removeAttr() issue
问题描述
$(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()
函数。见下文:
此外,在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代码 -
更新代码 -
Hands up - I can't figure it out what's wrong with it. Is that a bug or a wrong code ?
$(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"); };
}); });
So when there's no input the button returns false correctly - when there's an input in the field - still the button returns false, hence the removeAttr() doesn't work - why ? Regards.
Are you using jQuery 1.6.x?
If so then you should try using the .prop()
function. See below:
Disable/enable an input with jQuery?
Also, in your if statement no need to keep selecting $("#rem_but")
. Based on your code I would recommend $(this)
instead -
$(this).prop('disabled', true);
This should work -
$(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); }
});
});
Here is the working jsFiddle code -
Updated code -
这篇关于removeAttr()问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!