2011-12-23 198 views
0

我收到错误object property doesn't support this method当我尝试删除属性时,我加载页面。jQuery如何检查属性是否存在之前删除它

但是我相信会发生这种情况,因为在加载页面时从未添加禁用的属性,因为在添加属性后我没有收到错误。

我的问题是如何在试图删除它之前检查属性是否存在。

感谢

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(); 
+1

请参阅此问题: http://stackoverflow.com/questions/1318076/jquery-hasattr-checking-to-see-if-there-is-an-attribute-on-an-element – brandwaffle 2011-12-23 16:37:49

+5

你的选择器中有语法错误:'option ['value = ADD']'应该是'option [value ='ADD']'(对于其他'[name ='value']'选择器也是如此)。 – 2011-12-23 16:38:11

回答

5

可以使用.hasAttribute方法

如果你有各自的多个地址和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")) 

编辑:根据安德鲁的评论修正语法错误。

+0

他是对的 - 另外,使用像[firebug](http://www.getfirebug.com)或[开发人员工具](http://code.google.com/chrome/devtools/docs)这样的调试控制台确实很有帮助/overview.html#access)。控制台是你的朋友。 – Chazbot 2011-12-23 16:43:01

+0

感谢这工作,我猜的语法是问题 – 2011-12-23 16:47:31

+0

@bugz欢迎您:)高兴地帮助。 – 2011-12-23 17:07:28

相关问题