【问题标题】:Selecting radio button选择单选按钮
【发布时间】:2011-09-22 23:02:12
【问题描述】:

这是用于选择单选按钮的 jquery 脚本。

function radio(){
       var presetValue = "scholar";
$("[name=identity]").filter("[value="+presetValue+"]").attr("checked","checked");
}

脚本在页面加载时被调用。 这是 XHTML 代码的和平:

<label for="scholar"> Scholar </label>
        <input type="radio" name="identity" id="scholar" value="scholar"  />
        </td>

        <td>
        <label for="oracle"> Oracle </label>
        <input type="radio" name="identity" id="oracle" value="oracle"/>
        </td>

        <td>
        <label for="both"> Both </label>
        <input type="radio" name="identity" id="both" value="both"/>

当程序运行时,'scholar' 单选按钮未被选中。 可能是什么问题? 最好的问候

【问题讨论】:

    标签: jquery xhtml


    【解决方案1】:

    如果您使用 jQuery >= 1.6,您应该使用prop()

    另外,您使用复杂的选择器是徒劳的。由于您的输入具有 id,因此可以这样做:

    $('#scholar').prop('checked', true);
    

    如果presetValue 发生变化(即:您简化了代码),请执行以下操作:

    $('#' + presetValue).prop('checked', true);
    

    【讨论】:

      【解决方案2】:

      【讨论】:

        【解决方案3】:
        function radio(){
               var presetValue = "scholar";
        $("input[value="+presetValue+"]").attr("checked",true);
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-02-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多