Get value of custom attribute

好久不见. 提交于 2019-12-06 16:36:49

问题


I have two radio buttons. I would like to be able to get the value of the custom attribute "xmlvalue" of the checked radio button.

I have tried with the following script:

var userType = $("input[name=ctrl_CustomerType]:checked", this).attr('xmlvalue');

Markup:

<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

-- But I keep getting "Undefined".

Any ideas?


回答1:


Remove the context of your selector:

http://jsfiddle.net/NrQek/1/

 var userType = $("input[name=ctrl_CustomerType]:checked").attr('xmlvalue');
        alert("xmlvalue is: " + userType);



回答2:


Your selector is wrong.

The input element is not children of a element where you are clicking, so you cannot pass this as a context to the selector

var userType = $("input[name=ctrl_CustomerType]:checked").attr('xmlvalue');

Demo: Fiddle



来源:https://stackoverflow.com/questions/16916503/get-value-of-custom-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!