![](/img/trans.png)
[英]Is it necessary to check if a handler exists in a delegate chain before removing it?
[英]jQuery how to check if attribute exists before removing it
我在加载页面时尝试删除属性时遇到错误object property doesn't support this method
的错误。
但是,我相信这是因为没有在页面加载时添加禁用属性,因为添加属性后我没有收到错误。
我的问题是如何在尝试删除该属性之前检查该属性是否存在。
谢谢
if (jQuery.inArray($("select option:selected").val(), Codes) == -1) {
$(serviceSelector).hide();
$(LocationSelector).hide();
$("#ctl00_ctl00_body_body_ddlPool option['value=ADD']").removeAttr("disabled");
$("#ctl00_ctl00_body_body_ddlPool option['value=ADM']").removeAttr("disabled");
} else {
$(serviceSelector).show();
$(LocationSelector).show();
$("#ctl00_ctl00_body_body_ddlPool option[value=ADM]").attr("disabled", "disabled");
$("#ctl00_ctl00_body_body_ddlPool option[value=ADD]").attr("disabled", "disabled");
}
//$find(AcId).set_contextKey($(this).val());
}).change();
您可以使用.hasAttribute
方法
如果每个ADD和ADM节点中有多个,则必须测试每个节点:
$("#ctl00_ctl00_body_body_ddlPool option[value='ADD']").each(function(){
if(this.hasAttribute("disabled"))
this.removeAttribute("disabled");
});
否则,仅针对实际节点进行测试
if($("#ctl00_ctl00_body_body_ddlPool option[value='ADD']")[0].hasAttribute("disabled"))
编辑:根据安德鲁的评论修正语法错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.