jQuery 获取自定义属性的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16916503/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:03:27  来源:igfitidea点击:

Get value of custom attribute

jqueryradio-buttonattr

提问by Meek

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.

我有两个单选按钮。我希望能够获得选中单选按钮的自定义属性“xmlvalue”的值。

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?

有任何想法吗?

回答by A. Wolff

Remove the context of your selector:

删除选择器的上下文:

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

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

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

回答by Arun P Johny

Your selector is wrong.

你的选择器是错误的。

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

输入元素不是a您单击的元素的子元素,因此您无法将其this作为上下文传递给选择器

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

Demo: Fiddle

演示:小提琴