removeAttr()问题 [英] removeAttr() issue

查看:86
本文介绍了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/


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 -

http://jsfiddle.net/4rPc5/

Updated code -

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

这篇关于removeAttr()问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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