简体   繁体   English

如何通过jQuery获取字段集中的选定单选按钮值?

[英]How to get selected radio button value in a fieldset via jQuery?

I have 2 radio buttons wrapped in a fieldset. 我有两个单选按钮包装在一个字段集中。 I want to get the value of the clicked radio button inside the fieldset when it is clicked. 我想获取单击时字段集内单击的单选按钮的值。

HTML: HTML:

<fieldset id="skin-complexion" name="properties[skin_complexion]">

  <img src="{{ 'icon-fair.png' | asset_url }}">
  <input type="radio" value="Fair" />
  Fair

  <img src="{{ 'icon-medium.png' | asset_url }}">
  Medium
  <input type="radio" value="Medium" />


</fieldset>

jQuery: jQuery的:

$('#skin-complexion').on('change', function() {

  var value = $(this).val();
  alert(value);      

});

My jQuery code above is not working. 我上面的jQuery代码无法正常工作。 Any help is appreciated. 任何帮助表示赞赏。

You have to use radio as selector too because you are changing radio buttons 你必须使用radio作为选择也因为你正在改变单选按钮

$('#skin-complexion input:radio').on('change', function() {
  var value = $(this).val();
  alert(value);      
});

Working snippet:- 工作片段:-

 $('#skin-complexion input:radio').on('change', function() { var value = $(this).val(); console.log(value); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <fieldset id="skin-complexion" name="properties[skin_complexion]"> <img src="{{ 'icon-fair.png' | asset_url }}"> <input type="radio" value="Fair" /> Fair <img src="{{ 'icon-medium.png' | asset_url }}"> Medium <input type="radio" value="Medium" /> </fieldset> 

Note:- If you want that at a time only one radio button can be checked, then add common name attribute to them 注意:-如果您希望一次只能检查一个单选按钮,则可以向它们添加通用name属性

 $('#skin-complexion input:radio').on('change', function() { var value = $(this).val(); console.log(value); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <fieldset id="skin-complexion" name="properties[skin_complexion]"> <img src="{{ 'icon-fair.png' | asset_url }}"> <input type="radio" name="radio_example" value="Fair" /> Fair <img src="{{ 'icon-medium.png' | asset_url }}"> Medium <input type="radio" name="radio_example" value="Medium" /> </fieldset> 

Try $('#skin-complexion > input[type=radio]') . 尝试$('#skin-complexion > input[type=radio]')

You should also have to set a common name attribute to your radio buttons so that only one radio button can be selected at a time: 您还应该为单选按钮设置一个通用name属性,以便一次只能选择一个单选按钮:

 $('#skin-complexion > input[type=radio]').on('change', function() { var value = $(this).val(); alert(value); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <fieldset id="skin-complexion" name="properties[skin_complexion]"> <img src="{{ 'icon-fair.png' | asset_url }}"> <input type="radio" name="level" value="Fair" /> Fair <img src="{{ 'icon-medium.png' | asset_url }}"> Medium <input type="radio" name="level" value="Medium" /> </fieldset> 

You need to put the names to the radio button otherwise use can select both the radio buttons at the same time. 您需要将名称放在单选按钮上,否则可以同时选择两个单选按钮。 If users can select both radio button at the same time then you can try the below code 如果用户可以同时选择两个单选按钮,则可以尝试以下代码

$("#skin-complexion input[type='radio']").click(function(){
    console.log($(this).val());
});

And if user can select one radio button at the same time then you need to add names to the radio button like below: 如果用户可以同时选择一个单选按钮,则需要将名称添加到单选按钮中,如下所示:

<fieldset id="skin-complexion" name="properties[skin_complexion]">

  <img src="{{ 'icon-fair.png' | asset_url }}">
  <input type="radio" value="Fair" name="type1" />
  Fair

  <img src="{{ 'icon-medium.png' | asset_url }}">
  Medium
  <input type="radio" value="Medium" name="type1" />


</fieldset>

And use the following script to get the checked radio button value: 并使用以下脚本获取选中的单选按钮值:

$("#skin-complexion input[type='radio']").click(function(){
    console.log($(this).val());
});

Hope this helps! 希望这可以帮助!

I reccomand you learn about attribute in Jquery: https://api.jquery.com/attribute-equals-selector/ 我建议您了解Jquery中的attributehttps ://api.jquery.com/attribute-equals-selector/

 $('#skin-complexion input[type="radio"]').on('change', function() { var value = $(this).val(); alert(value); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <fieldset id="skin-complexion" name="properties[skin_complexion]"> <img src="{{ 'icon-fair.png' | asset_url }}"> <input type="radio" value="Fair" /> Fair <img src="{{ 'icon-medium.png' | asset_url }}"> Medium <input type="radio" value="Medium" /> </fieldset> 

You should give name attribute to your radio button if they are of same group. 如果它们属于同一组,则应将name属性赋予单选按钮。 Then you can select the value on click and alert it. 然后,您可以选择单击值并发出警报。

 $('input[type=radio][name=myRadios]').on('change', function() { var value = $(this).val(); alert(value); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <fieldset id="skin-complexion" name="properties[skin_complexion]"> <img src="{{ 'icon-fair.png' | asset_url }}"> <input type="radio" value="Fair" name="myRadios" /> Fair <img src="{{ 'icon-medium.png' | asset_url }}"> Medium <input type="radio" value="Medium" name="myRadios"/> </fieldset> 

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

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