使用 jQuery 获取单选按钮的当前选定值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6155379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting the currently selected value of a radio button with jQuery
提问by raoul1034
In MVC, I can define a set of radio buttons like so:
在 MVC 中,我可以定义一组单选按钮,如下所示:
@Html.RadioButtonFor(x => x.hasAcceptedAgreement, true) Yes
@Html.RadioButtonFor(x => x.hasAcceptedAgreement, false) No
which renders
这呈现
<input id="hasAcceptedAgreement" name="hasAcceptedAgreement" type="radio" value="True" /> Yes
<input id="hasAcceptedAgreement" name="hasAcceptedAgreement" type="radio" value="False" /> No
I want to disable or enable a div based on what item is selected, which I've managed to do using this bit of jQuery:
我想根据选择的项目禁用或启用一个 div,我已经设法使用这个 jQuery 来做到这一点:
$(':radio[name=hasAcceptedAgreement]').change(function () {
if ($(this).val() == 'True') {
// display div
} else {
// hide div
}
});
But when they submit the form, I also need to have a check to see which value is currently selected, then show/hide the appropriate section again (since the .change function won't be called on POST). Is there a way I can accomplish that?
但是当他们提交表单时,我还需要检查一下当前选择了哪个值,然后再次显示/隐藏适当的部分(因为不会在 POST 上调用 .change 函数)。有没有办法我可以做到这一点?
If I try to do if $(':radio[name=hasAcceptedAgreement]').val(), it will always evaluate to True since the ids are the same (I'm assuming). Is there some other way to get the value, or am I up creek because MVC likes to give the same name to each id?
如果我尝试执行 if $(':radio[name=hasAcceptedAgreement]').val(),它将始终评估为 True,因为 id 相同(我假设)。是否有其他方法可以获取该值,还是因为 MVC 喜欢为每个 id 赋予相同的名称,所以我不知道该怎么办?
EDIT: By the way, I am aware I can just manually set the ids in the RadioButtonFor call - I just wondered if there was some easier built-in way to handle it I wasn't aware of.
编辑:顺便说一句,我知道我可以在 RadioButtonFor 调用中手动设置 id - 我只是想知道是否有一些更简单的内置方法来处理它我不知道。
回答by JoseV
As they say here: How can I get which radio is selected via jQuery?you may use:
正如他们在这里所说:我怎样才能通过 jQuery 选择哪个收音机?你可以使用:
$('input[name=hasAcceptedAgreement]:checked').val()
to get the value of the currently selected radio.
获取当前选择的收音机的值。
回答by Yini
I used the code below in a project I worked on and it worked!
我在我从事的一个项目中使用了下面的代码,并且成功了!
$(':radio[name=hasAcceptedAgreement]').change(function () {
if ($('input[name=hasAcceptedAgreement]:checked').val() == 'True') {
// display div
} else {
// hide div
}
});
回答by roberkules
$(function(){
// executed once the dom is loaded
// bind the change function as usual
$(':radio[name=hasAcceptedAgreement]').change(function () {
if ($(this).val() == 'True') {
// display div
} else {
// hide div
}
}).change(); // and immediately trigger it
});
A small improvement for the change handler (so you don't need the if/else block)
更改处理程序的小改进(因此您不需要 if/else 块)
$("#yourdiv").toggle( $(this).val() == 'True' );