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

发布于 2024-09-10 04:25:22 字数 669 浏览 1 评论 0原文

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

我如何执行类似的操作

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

提前致谢...

given following 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/

how can I perform actions like

If radiobutton blue is checked do ...
If radiobutton green is checked do...

thanks in advance...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一生独一 2024-09-17 04:25:22

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

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

您可以在这里测试

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

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

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

Use the attribute-equals (for the name match) and :checked selectors, like this:

$(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.

Since you seem to be in ASP.Net though, you'll need a selector like this in your page:

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

This is the attribute-ends-with selector, to handle the naming container prefixes that ASP.Net appends.

残疾 2024-09-17 04:25:22

抓住选中的项目并测试其值。

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

});

grab the checked item and test against its value.

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

});

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文