Javascript JQuery:如何获取选定的单选按钮值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4138859/
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
JQuery: How to get selected radio button value?
提问by StaceyH
How do I default the value of a non-selected radio button to 0
?
如何将未选中的单选按钮的值默认为0
?
I have the following HTML:
我有以下 HTML:
<input type="radio" name="myradiobutton" value="1" />1
<input type="radio" name="myradiobutton" value="2" />2
<input type="radio" name="myradiobutton" value="3" />3
And I have the following code to get the value of the selectedradio button
我有以下代码来获取所选单选按钮的值
var radio_button_value $('input[name=myradiobutton]:radio').click(function() {var value = $(this).val();});
Problem is, I want to default the value of radio_button_value
to 0
in the event nothingis selected. How do I do that?
问题是,如果没有选择任何内容,我想默认为radio_button_value
to的值。我怎么做?0
I basically want to do the pseduo-code below (but this doesn't work)
我基本上想做下面的伪代码(但这不起作用)
var radio_button_value $('input[name=myradiobutton]:radio').click(function() {
if set
return $(this).val();
else
return 0;
});
UPDATE
更新
There appears to be confusion on what I'm asking for.
我的要求似乎有些混乱。
What I want to do when the page loads (before a user has had a chance to even select a radio button), I want a function that will return 0 for the radio button value.
当页面加载时(在用户甚至有机会选择一个单选按钮之前),我想要做什么,我想要一个函数,该函数将为单选按钮值返回 0。
Then, once a user selected a radio button, that SAME function will return the selected value of the radio button.
然后,一旦用户选择了单选按钮,该 SAME 函数将返回单选按钮的选定值。
Make sense?
有道理?
回答by John Hartsock
$('input[name=myradiobutton]:radio:checked')
will get you the selected radio button
$('input[name=myradiobutton]:radio:not(:checked)')
will get you the unselected radio buttons
$('input[name=myradiobutton]:radio:checked')
将为您提供选定的单选按钮
$('input[name=myradiobutton]:radio:not(:checked)')
将为您提供未选择的单选按钮
Using this you can do this
使用这个你可以做到这一点
$('input[name=myradiobutton]:radio:not(:checked)').val("0");
Update:After reading your Update I think I understand You will want to do something like this
更新:阅读您的更新后,我想我明白您会想要做这样的事情
var myRadioValue;
function radioValue(jqRadioButton){
if (jqRadioButton.length) {
myRadioValue = jqRadioButton.val();
}
else {
myRadioValue = 0;
}
}
$(document).ready(function () {
$('input[name=myradiobutton]:radio').click(function () { //Hook the click event for selected elements
radioValue($('input[name=myradiobutton]:radio:checked'));
});
radioValue($('input[name=myradiobutton]:radio:checked')); //check for value on page load
});
回答by Dexter
Use the :checkedselector to determine if a value is selected:
使用:checked选择器来确定是否选择了一个值:
function getRadioValue () {
if( $('input[name=myradiobutton]:radio:checked').length > 0 ) {
return $('input[name=myradiobutton]:radio:checked').val();
}
else {
return 0;
}
}
Updateyou can call the function above at any time to get the selected value of the radio buttons. You can hook into it on load and then whenever the value changes with the following events:
更新您可以随时调用上面的函数来获取单选按钮的选定值。您可以在加载时挂钩它,然后每当值随以下事件发生变化时:
$(document).ready( function() {
// Value when you load the page for the first time
// Will return 0 the first time it's called
var radio_button_value = getRadioValue();
$('input[name=myradiobutton]:radio').click( function() {
// Will get the newly selected value
radio_button_value = getRadioValue();
});
}
回答by Ashish Babu
It will start as soon as the page loads. You can keep it under some events like button click
它会在页面加载后立即启动。您可以将其保留在按钮单击等某些事件下
$("#btn").click(function() {
var val= $('input[type="radio"]:checked').val();
});
回答by Muhammad Adnan
jQuery("input:radio[name=myradiobutton]:checked").val();
回答by Ender
Probably the best method (particularly if you want to be able to post a default value), would be to have a hidden radio button that starts out as selected and has your default value. So something like this:
可能最好的方法(特别是如果您希望能够发布默认值时),是有一个隐藏的单选按钮,该按钮开始时被选中并具有您的默认值。所以像这样:
<input type="radio" name="myradiobutton" value="0" checked="checked" style="display:none;" />
<input type="radio" name="myradiobutton" value="1" />1
<input type="radio" name="myradiobutton" value="2" />2
<input type="radio" name="myradiobutton" value="3" />3
If you can't modify your html directly, you could add it via script:
如果你不能直接修改你的 html,你可以通过脚本添加它:
$(function() {
$('input[name=myradiobutton]:radio:first').before('<input type="radio" name="myradiobutton" value="0" checked="checked" style="display:none;" />');
});
Here's a demo of that: http://jsfiddle.net/Ender/LwPCv/
这是一个演示:http: //jsfiddle.net/Ender/LwPCv/
回答by arunsignit
This Jquery method returns the default vale 0 when page loads...
当页面加载时,此 Jquery 方法返回默认值 0...
$('input[type="radio"]:checked').val();
回答by mindmyweb
I know this is an old question but I find that the answers are not sufficent enough
我知道这是一个老问题,但我发现答案还不够
The cleanest way to do this is to use this code
最干净的方法是使用此代码
$(".myRadio").click(function() {
alert($(this).val());
});
:-)
:-)
Your form input data should look like this
您的表单输入数据应如下所示
<input type="radio" value="40000" name="pay" class="myRadio" />
<label for="40000">40000</label>
回答by Nadir
To get the value of the selected Radio Button, Use RadioButtonName and the Form Id containing the RadioButton.
要获取所选单选按钮的值,请使用 RadioButtonName 和包含 RadioButton 的 Form Id。
$('input[name=radioName]:checked', '#myForm').val()
OR by only
或仅由
$('form input[type=radio]:checked').val();
回答by saqibahmad
This work for me hope this will help: to get radio selected value you have to use ratio name as selector like this
这项工作对我来说希望这会有所帮助:要获得无线电选择值,您必须像这样使用比率名称作为选择器
selectedVal = $('input[name="radio_name"]:checked').val();
selectedVal will have the required value, change the radio_name according to yours, in your case it would b "myradiobutton"
selectedVal 将具有所需的值,根据您的值更改 radio_name,在您的情况下它将是“myradiobutton”
selectedVal = $('input[name="myradiobutton"]:checked').val();
回答by Gregg B
You should really be using checkboxes if there will be an instance where something isn't selected.
如果存在未选择某些内容的实例,则您确实应该使用复选框。
according to the W3C
根据 W3C
If no radio button in a set sharing the same control name is initially "on", user agent behavior for choosing which control is initially "on" is undefined. Note. Since existing implementations handle this case differently, the current specification differs from RFC 1866 ([RFC1866] section 8.1.2.4), which states:
At all times, exactly one of the radio buttons in a set is checked. If none of the elements of a set of radio buttons specifies `CHECKED', then the user agent must check the first radio button of the set initially.
Since user agent behavior differs, authors should ensure that in each set of radio buttons that one is initially "on".
如果共享相同控件名称的集合中没有单选按钮最初为“on”,则用于选择哪个控件最初为“on”的用户代理行为未定义。笔记。由于现有实现对这种情况的处理方式不同,当前规范与 RFC 1866([RFC1866] 第 8.1.2.4 节)不同,其中指出:
在任何时候,只检查一组单选按钮中的一个。如果一组单选按钮的元素都没有指定“已检查”,则用户代理必须首先检查该组的第一个单选按钮。
由于用户代理行为不同,作者应确保在每组单选按钮中最初是“打开”的。