最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get selected radio button value in a fieldset via jQuery? - Stack Overflow

programmeradmin1浏览0评论

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:

<fieldset id="skin-plexion" name="properties[skin_plexion]">

  <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:

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

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

});

My jQuery code above is not working. Any help is appreciated.

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:

<fieldset id="skin-plexion" name="properties[skin_plexion]">

  <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:

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

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

});

My jQuery code above is not working. Any help is appreciated.

Share Improve this question edited May 16, 2018 at 9:54 Mamun 68.9k9 gold badges51 silver badges62 bronze badges asked May 16, 2018 at 9:26 AdrianAdrian 3,0624 gold badges37 silver badges74 bronze badges 3
  • Have you tried $('#skin-plexion input')? – deEr. Commented May 16, 2018 at 9:29
  • Conan Carroll did you checked my answer? – Death-is-the-real-truth Commented May 16, 2018 at 13:13
  • @AlivetoDie yes. your answer is checked now. thank you! – Adrian Commented May 16, 2018 at 17:58
Add a ment  | 

5 Answers 5

Reset to default 4

You have to use radio as selector too because you are changing radio buttons

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

Working snippet:-

$('#skin-plexion input:radio').on('change', function() {
  var value = $(this).val();
  console.log(value);      
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<fieldset id="skin-plexion" name="properties[skin_plexion]">

  <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 mon name attribute to them

$('#skin-plexion input:radio').on('change', function() {
  var value = $(this).val();
  console.log(value);      
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<fieldset id="skin-plexion" name="properties[skin_plexion]">

  <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-plexion > input[type=radio]').

You should also have to set a mon name attribute to your radio buttons so that only one radio button can be selected at a time:

$('#skin-plexion > input[type=radio]').on('change', function() {

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

});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<fieldset id="skin-plexion" name="properties[skin_plexion]">

  <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-plexion 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-plexion" name="properties[skin_plexion]">

  <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-plexion input[type='radio']").click(function(){
    console.log($(this).val());
});

Hope this helps!

I recand you learn about attribute in Jquery:https://api.jquery./attribute-equals-selector/

$('#skin-plexion input[type="radio"]').on('change', function() {

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

});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<fieldset id="skin-plexion" name="properties[skin_plexion]">

  <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. 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./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<fieldset id="skin-plexion" name="properties[skin_plexion]">

  <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>

发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>