获取自定义属性的值
Posted
技术标签:
【中文标题】获取自定义属性的值【英文标题】:Get value of custom attribute 【发布时间】:2013-05-30 18:29:33 【问题描述】:我有两个单选按钮。我希望能够获取选中的单选按钮的自定义属性“xmlvalue”的值。
我已尝试使用以下脚本:
var userType = $("input[name=ctrl_CustomerType]:checked", this).attr('xmlvalue');
标记:
<input type="radio" name="ctrl_CustomerType" id="ctrl_CustomerType_1" xmltag="CustomerType" xmlvalue="existingCustomer" checked="checked"> Yes
<br />
<input type="radio" name="ctrl_CustomerType" id="ctrl_CustomerType_2" xmltag="CustomerType" xmlvalue="newCustomer"> No
Fiddle here
-- 但我不断收到“未定义”。
有什么想法吗?
【问题讨论】:
【参考方案1】:删除选择器的上下文:
http://jsfiddle.net/NrQek/1/
var userType = $("input[name=ctrl_CustomerType]:checked").attr('xmlvalue');
alert("xmlvalue is: " + userType);
【讨论】:
我不知道我在想什么。非常感谢。它按预期工作。【参考方案2】:您的选择器错误。
输入元素不是您单击的a
元素的子元素,因此您不能将this
作为上下文传递给选择器
var userType = $("input[name=ctrl_CustomerType]:checked").attr('xmlvalue');
演示:Fiddle
【讨论】:
以上是关于获取自定义属性的值的主要内容,如果未能解决你的问题,请参考以下文章