简体   繁体   English

JQuery:如何获得选中的单选按钮值?

[英]JQuery: How to get selected radio button value?

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 selected radio 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 nothing is selected. 问题是,我想在未选择任何内容的情况下将radio_button_value的值默认为0 How do I do that? 我怎么做?

I basically want to do the pseduo-code below (but this doesn't work) 我基本上想做下面的pseduo代码(但这不起作用)

var radio_button_value $('input[name=myradiobutton]:radio').click(function() { 
if set
  return $(this).val();
else
  return 0;
});

UPDATE 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? 合理?

$('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
});

Use the :checked selector 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;
    }
}

Update you 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();
    });
}

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();
});
jQuery("input:radio[name=myradiobutton]:checked").val();

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>

当页面加载时,此Jquery方法返回默认值0

$('input[type="radio"]:checked').val();

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();

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/

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();

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: 由于现有实现以不同方式处理这种情况,因此当前规范与RFC 1866([RFC1866]第8.1.2.4节)不同,后者指出:

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. 如果一组单选按钮中没有一个元素指定“CHECKED”,则用户代理必须首先检查该集合的第一个单选按钮。

Since user agent behavior differs, authors should ensure that in each set of radio buttons that one is initially "on". 由于用户代理行为不同,作者应确保在每组单选按钮中最初一个“开启”。

For radio buttons use the following script: 对于单选按钮,请使用以下脚本:

var myRadio = $('input[name=meme_wall_share]');
var checkedValue = myRadio.filter(':checked').val();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM