Javascript JQuery 从单选按钮更改值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9010805/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 08:22:53  来源:igfitidea点击:

JQuery change value from radio button

javascriptjqueryradio-button

提问by Nacho Sarmiento

I have a variable called categorythat must change its value according to the selected radio button. The problem is that I do not get that at the click of a radio button or another, the value will change instantly.

我有一个变量category,它必须根据选定的单选按钮更改其值。问题是我没有在单击单选按钮或其他按钮时得到它,该值会立即改变。

This is the code I have:

这是我的代码:

<form>
<input type="radio" name="category" value="doctor" /> Doctor
<input type="radio" name="category" value="police" checked /> Policia
</form>

var category = $('[name="category"]:checked').val();

I need you to automatically change the value of the variable categorywhen you click the radio buttons.

我需要您category在单击单选按钮时自动更改变量的值。

回答by Blender

Attach an event handler to the changeevent:

将事件处理程序附加到change事件:

$(':radio[name="category"]').change(function() {
  var category = $(this).filter(':checked').val();
});

回答by Jose Vega

You need to change the value of category every time a user changes the selected radio button. Therefore you will need an event to trigger when the user clicks on a radio. You can check out the FIDDLE

每次用户更改选定的单选按钮时,您都需要更改 category 的值。因此,当用户单击收音机时,您将需要一个事件来触发。您可以查看FIDDLE

var category = null;
$("input[name='category']").click(function() {
    category = this.value;
});

回答by albanx

I think you need simply something like this:

我认为你只需要这样的东西:

var category;

    $('radio[name="category"]').click(function(){
      category=this.value;
    });

回答by nima shayanfar

var category;
    $(':radio[name="category"]').change(function() {
      category=this.value;
    });

And this is a jsfiddle.netdemo.

这是一个jsfiddle.net演示。

回答by ala'a alqadi

You shouldn't give the same ID for more than one element, you should use class instead like this :

你不应该为多个元素提供相同的 ID,你应该像这样使用 class:

<form>
  Option 1<input type="radio" name="opt" class="radio" value="Option 1">
  Option 2<input type="radio" name="opt" class="radio" value="Option 2">
</form>

instead of :

代替 :

<form>
  Option 1<input type="radio" name="opt" id="radio" value="Option 1">
  Option 2<input type="radio" name="opt" id="radio" value="Option 2">
</form>

and your code still the same and it should work fine :)

并且您的代码仍然相同,它应该可以正常工作:)

回答by ShankarSangoli

Instead of using categoryvariable use $('[name="category"]:checked').val()wherever you want the value of the checked radio button. Otherwise you can make use of changeor clickevent to set the checkedradio buttons value into categoryvariable.

不要在任何需要选中单选按钮的值的地方使用category变量$('[name="category"]:checked').val()。否则,您可以使用changeclick事件将checked单选按钮值设置为category变量。

回答by bdparrish

utilize the .change()in order to capture the change event for a radio button.

利用.change()来捕获单选按钮的更改事件。