jquery 检查 onload 选中了哪个单选按钮

标签 jquery radio-button onload

给定以下 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/

我如何执行类似的操作

如果选中蓝色单选按钮,则... 如果选中绿色单选按钮,则...

提前致谢...

最佳答案

使用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 附加的命名容器前缀。

关于jquery 检查 onload 选中了哪个单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3237444/

相关文章:

javascript - 如何在 jQuery 中将图像添加为圆 Angular 的背景?

asp.net - 通过响应流将文件发送到浏览器后解除阻止(jQuery BlockUI)

c# - C#中单选按钮事件的顺序

javascript - onload javascript将背景图像分配给div

asp.net - 如何构建专门的 JQuery 计时器

jquery - 在jQuery UI对话框中打开http://www.youtube.com

python - 在views.py django中检索多个单选按钮和表单字段的值

php - 使用 PHP 将单选按钮值添加到 MYSQL 表

javascript - 在单独的文件中使用多个 window.onload 事件

javascript - 如何仅在从 REST Api 获取所有数据后才执行函数