无法使用jQuery正确更新属性

时间:2014-12-27 04:46:43

标签: javascript jquery polymer

我正在使用聚合物和jquery来更改标签的属性,如果一个标签的属性为false,使用if / else语句,目前我正在使用的那个不起作用,我将如何使其工作?

HTML

<p flex>Would you like notifications on?</p> <paper-toggle-button checked="false" id="onoff"></paper-toggle-button>
<p flex>Private Message Notifications?</p> <paper-toggle-button checked="true" id="pmn" class="blue"></paper-toggle-button>
<p flex>Game Update Notifications?</p> <paper-toggle-button checked="true" id="gun" class="lightgrey"></paper-toggle-button>
<p flex>Website Update Notifications?</p> <paper-toggle-button checked="true" id="wun" class="pink"></paper-toggle-button>

的JavaScript

var main = document.querySelector('#onoff');
var pmn = document.querySelector('#pmn');
var wun = document.querySelector('#wun');
var gun = document.querySelector('#gun');

if ( $('#onoff').attr('checked') == 'true' ) {
    // do this
} else {
    pmn.removeAttribute("checked");
    wun.removeAttribute("checked");
    gun.removeAttribute("checked");
    pmn.setAttribute("disabled", "true");
    wun.setAttribute("disabled", "true");
    gun.setAttribute("disabled", "true");

}

我目前有#onoff checked="false"因此其他切换会自动被禁用,属性checked会等于假,但事实并非如此,任何人都可以告诉我我是什么做错了,怎么解决?

2 个答案:

答案 0 :(得分:0)

像这样改变:

pmn.attr("checked", false);
wun.attr("checked", false);
gun.attr("checked", false);
pmn.attr("disabled", "true");
wun.attr("disabled", "true");
gun.attr("disabled", "true");

如果您使用的是jQuery v1.6 +

,请使用此选项
pmn.prop("checked", false);
wun.prop("checked", false);
gun.prop("checked", false);
pmn.prop("disabled", true);
wun.prop("disabled", true);
gun.prop("disabled", true);

答案 1 :(得分:0)

jQuery(document).ready(function($){
var main = document.querySelector('#onoff');
var pmn = document.querySelector('#pmn');
var wun = document.querySelector('#wun');
var gun = document.querySelector('#gun');

if ( $('#onoff').attr('checked') === 'true' ) {

   alert("Your book is overdue1.");
} else {
    //alert("Your book is overdue2.");
    pmn.removeAttribute("checked");
    wun.removeAttribute("checked");
    gun.removeAttribute("checked");
    pmn.setAttribute("disabled", "true");
    wun.setAttribute("disabled", "true");
    gun.setAttribute("disabled", "true");

}
});