简体   繁体   English

jQuery .attr()将值设置为未定义,为什么?

[英]Jquery .attr() sets value as undefined, why?

<div class="form-group">
                    <label>Email Visible (Fill other means of contact to change)</label>
                    <select id="emailVisible" required name="emailVisible" class="form-control" disabled="disabled">
                        <option value="yes" selected = "selected">Yes</option>
                        <option value="no">No</option>
                    </select>
                </div>

setInterval(function(){
            console.log("GOOO");
            if ($("#twitterInput").val() == "" && $("#facebookInput").val() == "" && $("#steamInput").val() == "") {
                console.log("1");
                $("#emailVisible").attr("disabled","disabled");
            } else {
                console.log("2");
                $("#emailVisible").attr("disabled",false);
                console.log($('#emailVisible').attr('disabled'));
            }                   
        }, 1000);

"console.log($('#emailVisible').attr('disabled'));" “ console.log($('#emailVisible')。attr('disabled'));” prints undefined in the console. 打印在控制台中未定义。

"2" is printed every second. 每秒打印“ 2”。

I do not know what is incorrect in my syntax. 我不知道我的语法不正确。

Try with prop() instead of attr() .And change the value with boolean type not with string .And dont forget to add 3 input 尝试使用prop()而不是attr() 。并使用布尔类型而不是string更改值,并且不要忘记添加3个输入

 setInterval(function() { if ($("#twitterInput").val() == "" && $("#facebookInput").val() == "" && $("#steamInput").val() == "") { console.log("1"); $("#emailVisible").prop("disabled", true); } else { console.log("2"); $("#emailVisible").prop("disabled", false); console.log($('#emailVisible').prop('disabled')); } }, 1000); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="form-group"> <input id="twitterInput"> <input id="facebookInput"> <input id="steamInput"> <label>Email Visible (Fill other means of contact to change)</label> <select id="emailVisible" required name="emailVisible" class="form-control" disabled="disabled"> <option value="yes" selected = "selected">Yes</option> <option value="no">No</option> </select> </div> 

You can observe that there will be no disabled attribute in <select> with id="emailVisible when you execute the jquery 您可以观察到,当执行jquery时, <select>没有id="emailVisible disabled属性。

 $("#emailVisible").attr("disabled",false);

This is because it removes this attribute to make the selectbox enable. 这是因为它删除了此属性以启用选择框。 That is why when you access $('#emailVisible').attr('disabled') it is undefined as there is no such attribute. 这就是为什么当您访问$('#emailVisible').attr('disabled')它是undefined因为没有这样的属性。 You can verify yourself by using inspect element on this selectbox. 您可以通过使用此选择框上的inspect element来验证自己。

 setInterval(function(){ console.log("GOOO"); if ($("#twitterInput").val() == "" && $("#facebookInput").val() == "" && $("#steamInput").val() == "") { console.log("1"); $("#emailVisible").prop("disabled","disabled"); } else { console.log("2"); $("#emailVisible").prop("disabled",false); console.log($('#emailVisible').prop('disabled')); } }, 1000); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="form-group"> <label>Email Visible (Fill other means of contact to change)</label> <select id="emailVisible" required name="emailVisible" class="form-control" disabled="disabled"> <option value="yes" selected = "selected">Yes</option> <option value="no">No</option> </select> </div> 

Just change the attr to prop as disable is a property. 只需将attr更改为prop即可,因为disable是一个属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM