简体   繁体   English

当我单击单选按钮时,它的true或自动其他Span值将变为false

[英]When i click on radio button then its true or automatically other Span value goes to false

When I clicked on the radio button, the span text changes to true or other should be false . 当我单击单选按钮时, span文本更改为true或其他应为false

截图

This code works perfectly fine: 这段代码可以正常工作:

 $('table').on('change', ':radio', function() { $(this).next('span').text('True').closest('td') .siblings().find('span').text('False'); }); 
 <tr> <td><input type="radio" name="attandance"><span>True</span></td> <td><input type="radio" name="attandance"><span>True</span></td> <td><input type="radio" name="attandance"><span>True</span></td> </tr> 

When I click on the radio button, it changes to "True" and when we click on the other radio button on the same row it remains the same. 当我单击单选按钮时,它更改为“ True”,而当我们单击同一行中的另一个单选按钮时,它保持不变。

I want to convert it back again, true to false. 我想再次将其转换为true到false。

Hope you all got my point. 希望大家都明白我的意思。

The problem is that radio button is always selected because change event is triggered on selected one not on all of them. 问题在于,单选按钮始终处于选中状态,因为更改事件是在选定的一个而不是所有这些上触发的。 So you need to set correct to True and the rest of the them to False, this is how I would do it: 因此,您需要将正确设置为True,其余的设置为False,这就是我的方法:

 $('table').on('change', ':radio', function () { $(this).next('span').text('True').closest('td') .siblings().find('span').text('False'); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table> <tr> <td> <input type='radio' name='attandance' ><span>False</span></td> <td> <input type='radio' name='attandance' ><span>False</span></td> <td> <input type='radio' name='attandance' ><span>False</span></td> </tr> </table> 

EDIT : And a bonus you can use this without JS: 编辑 :和奖金,您可以不使用JS使用此:

 input[type="radio"]:not(:checked) + span::before { content: 'False'; } input[type="radio"]:checked + span::before { content: "True"; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table> <tr> <td> <input type='radio' name='attandance' ><span></span></td> <td> <input type='radio' name='attandance' ><span></span></td> <td> <input type='radio' name='attandance' ><span></span></td> </tr> </table> 

You need to reset the text of other span elements of the same table row as the radio button being clicked. 您需要重置与单击单选按钮相同的表行中其他跨度元素的文本。 Consider using the closest() method as shown below to achieve this: 考虑使用如下所示的closest()方法来实现此目的:

 $('table').on('change', ':radio', function() { /* Access the closest tr ancestor to the radio button being clicked */ var tr = $(this).closest('tr'); /* Reset all span descendants for the anscestor tr to the default text of false */ $('span', tr).text('false'); /* Update the span relative to the checked radio button as you currently are */ $(this).next('span').text('true'); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> <table> <tbody> <tr> <td> John </td> <td> <input type='radio' name='attandance0'><span>false</span></td> <td> <input type='radio' name='attandance0'><span>false</span></td> <td> <input type='radio' name='attandance0'><span>false</span></td> </tr> <tr> <td> Peter </td> <td> <input type='radio' name='attandance1'><span>false</span></td> <td> <input type='radio' name='attandance1'><span>false</span></td> <td> <input type='radio' name='attandance1'><span>false</span></td> </tr> <tr> <td> Andrew </td> <td> <input type='radio' name='attandance2'><span>false</span></td> <td> <input type='radio' name='attandance2'><span>false</span></td> <td> <input type='radio' name='attandance1'><span>false</span></td> </tr> </tbody> </table> 

you just need to reset each radio button on change... 您只需要在更改时重置每个单选按钮即可...

  $('table').on('change', ':radio', function () { $(this).parents('table').find('span').text('false'); if($(this). prop("checked") == true){ $(this).next('span').text('true') } }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table> <tr> <td> <input type='radio' name='attandance' value="ch" /><span>false </span></td> <td> <input type='radio' name='attandance' /><span>false </span></td> <td> <input type='radio' name='attandance' /><span> false</span></td> </tr> </table> 

暂无
暂无

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

相关问题 如果自动检查跨度为真单选按钮的值 - if value of span true radio button automatically checked 当我尝试单击一个单选按钮时,它将选择另一个 - When I try to click one radio button it selects the other JavaScript 根据单选按钮显示/隐藏 div。 第一个实例与另一个实例冲突。 当我 select true/false 一个时,它适用于两者 - JavaScript show/hide div based on radio button. First instance conflicts with other one. When I select true/false on one, it applies to both 如果我单击选择按钮并在文本框中传递其值,如何获得所选单选按钮的值? - how can i get the value of selected radio button if i click the select button and pass its value in a textbox? 如果未选择其他值,则返回false不会取消单选按钮中的单选按钮 - Return false doesn't cancel radio buttons click in Chrome when no other value selected 单击确认对话框按钮时,如何返回true或false? - How may I return true or false when I click on confirm dialog button? 单选按钮管理 - 当我选择一个时,它会自动选择另一个 jQuery - Radio button management - when I choose one, it automatically selects the other jQuery 对Java中的单选按钮获取true或false - Get true or false for radio button in Javascript Reactjs:我正在检查基于 state 的单选按钮,但在选中一个单选按钮时无法单击其他单选按钮 - Reactjs: I am checking radio button based on state but can't click other radio button when one is checked 按钮单击时选中设置单选按钮为true - Setting radio button checked true on button click
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM