[英]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.