【问题标题】:how to get radio button value using jquery如何使用jquery获取单选按钮值
【发布时间】:2018-07-27 20:45:23
【问题描述】:
<input id="user_type_local" type="radio" checked="true" value="local" name="data[user_type]">
Local
<input id="user_type_sso" type="radio" value="sso" name="data[user_type]">
Gilead SSO

我们如何使用 jQuery 获取选中的单选按钮的值?我试过了

 $('input[name=data[user_type]]:radio').val();

但它不起作用。

错误:语法错误,无法识别的表达式:input[name=data[user_type]]:radio

【问题讨论】:

  • 有什么理由不使用id 属性? $( "#user_type_sso" ).val()

标签: jquery radio-button


【解决方案1】:

你应该用引号包裹字符串:

$('input[name="data[user_type]"]:radio').val();

这与使用双重转义相同:

$('input[name=data\\[user_type\\]]:radio').val();

【讨论】:

    【解决方案2】:

    []meta-characters,您必须将其转义,或者您可以简单地将其作为字符串。

    试试,

    $('input[name="data[user_type]"][type="radio"]').val();
    

    【讨论】:

      【解决方案3】:
      $("input:radio[name=theme]").click(function() {
          var value = $(this).val();
      });
      

      【讨论】:

        猜你喜欢
        • 2015-09-09
        • 1970-01-01
        • 2011-05-07
        • 1970-01-01
        • 1970-01-01
        • 2012-06-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多