问题
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