【问题标题】:jquery check onload which radiobutton is checkedjquery check onload 检查了哪个单选按钮
【发布时间】:2010-07-13 21:17:02
【问题描述】:

给出以下html

<input id="IBE1_IBE_NurFlug1_RadioButtonList1_0" 
       name="IBE1$IBE_NurFlug1$RadioButtonList1" value="Blue" type="radio">
<label for="IBE1_IBE_NurFlug1_RadioButtonList1_0">Blue</label>

<input id="IBE1_IBE_NurFlug1_RadioButtonList1_1" 
       name="IBE1$IBE_NurFlug1$RadioButtonList1" value="Green" checked="checked" 
       type="radio">
<label for="IBE1_IBE_NurFlug1_RadioButtonList1_1">Green</label>

http://jsfiddle.net/pCBJL/2/

我怎样才能执行类似的操作

如果选中蓝色单选按钮... 如果选中绿色单选按钮,请执行...

提前谢谢...

【问题讨论】:

    标签: jquery radio-button onload


    【解决方案1】:

    使用attribute-equals(用于名称匹配)和:checked 选择器,如下所示:

    $(function() {
      var val = $("input[name='IBE1$IBE_NurFlug1$RadioButtonList1']:checked").val();
      if(val === "Blue") {
        //do something
      } else {
        //do something else
      }
    });
    

    You can test it here.

    由于您似乎在 ASP.Net 中,您的页面中需要这样的选择器:

    $("input[name$='RadioButtonList1']:checked").val();
    

    这是attribute-ends-with selector,用于处理 ASP.Net 附加的命名容器前缀。

    【讨论】:

    • 非常感谢。两个答案都是正确的。 asp.net 提示是最重要的。
    • 谢谢...为我节省了大量时间
    【解决方案2】:

    获取选中的项目并根据其值进行测试。

    $(document).ready(function() {
    switch ($('input:checked').val()){
        case 'Green':
            //do something green
            break;
        case 'Blue':
            //do something blue
            break;
        default:
            //do something else
    }
    

    });

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-25
      • 2013-06-11
      • 2016-02-01
      相关资源
      最近更新 更多