【问题标题】: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

-- 但我不断收到“未定义”。

有什么想法吗?

【问题讨论】:

    标签: jquery radio-button attr


    【解决方案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

    【讨论】:

      猜你喜欢
      • 2023-03-03
      • 2015-05-06
      • 1970-01-01
      • 2013-04-16
      • 2011-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多