给定以下 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>
我如何执行类似的操作
如果选中蓝色单选按钮,则... 如果选中绿色单选按钮,则...
提前致谢...
最佳答案
使用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 selector ,处理 ASP.Net 附加的命名容器前缀。
关于jquery 检查 onload 选中了哪个单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3237444/