简体   繁体   English

单选按钮从选中状态变为未选中状态时该怎么做?

[英]How to do something when radio button is going from checked to unchecked?

I am trying to create a function in Javascript that changes an attribute on a radio button (This is so that I can detect what radio button is checked) when someone clicks off/on it. 我正在尝试用Javascript创建一个函数,当有人单击它时,它会更改单选按钮的属性(这样我就可以检测到选中了哪个单选按钮)。 Here is a jsFiddle explaining a little more of what I am looking to do. 这是一个jsFiddle,解释了我想要做的更多事情。 The only problem with the code in the jsFiddle is that it is in jQuery and I do not understand enough jQuery to convert it back to its pure Javascript counterpart. jsFiddle中的代码唯一的问题是它在jQuery中,并且我对jQuery的理解不足,无法将其转换回纯Javascript。 Here is my attempt to convert jQuery to Javascript. 是我尝试将jQuery转换为Javascript的尝试。 I could totally just copy the code and just use it but then I would not be learning anything. 我完全可以只复制代码并使用它,但那时我什么都不会学。

I have been trying to figure this out for the last 4 ish hours and would really appreciate any help. 在过去的4个小时内,我一直在努力弄清楚这一点,非常感谢您的帮助。

Thanks, Alex 谢谢,亚历克斯

 InitRadio('name'); function InitRadio(name) { val = 0; $.each($(':radio[name="' + name + '"]'), function() { $(this).val(val++); $(this).attr('chk', '0'); $(this).on("click", function(event) { SetRadioButtonChkProperty($(this).val(), name); document.getElementById('1').innerText = document.getElementById('input1').getAttribute('chk'); document.getElementById('2').innerText = document.getElementById('input2').getAttribute('chk'); document.getElementById('3').innerText = document.getElementById('input3').getAttribute('chk'); }); }); document.getElementById('1').innerText = document.getElementById('input1').getAttribute('chk'); document.getElementById('2').innerText = document.getElementById('input2').getAttribute('chk'); document.getElementById('3').innerText = document.getElementById('input3').getAttribute('chk'); } function SetRadioButtonChkProperty(val, name) { $.each($(':radio[name="' + name + '"]'), function() { if ($(this).val() != val) $(this).attr('chk', '0'); else { if ($(this).attr('chk') == '0') $(this).attr('chk', '1'); } }); } 
 p { display: inline; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> <input type='radio' class='radio-button' name='name' id="input1"> <p id="1"></p> <input type='radio' class='radio-button' name='name' id="input2"> <p id="2"></p> <input type='radio' class='radio-button' name='name' id="input3"> <p id="3"></p> 

Maybe this is what you want. 也许这就是您想要的。
You can try it out in this Fiddle 你可以在这个小提琴中尝试一下

HTML: HTML:

<div class="RadioList"> 
  <input type="radio" class="radioBtn" value="r1" name="radio"> R1
  <input type="radio" class="radioBtn" value="r2" name="radio"> R2
  <input type="radio" class="radioBtn" value="r3" name="radio"> R3
  <input type="radio" class="radioBtn" value="r4" name="radio"> R4 
</div> 
<div class="statusRadio">
  <p>Checked Radio value: <b id="statusChecked">none</b></p>
</div>

jQuery: jQuery的:

$(document).ready(function() { 
  $(document).on('click', '.radioBtn', function() {
    var valRadio = this.value;
    $('#statusChecked').html(valRadio);
  });
});

My understanding is that you're partially through converting jQuery to javascript and you're having difficulty with the conversion due to lack of understanding about jQuery. 我的理解是,您正在部分地通过将jQuery转换为javascript来进行转换,并且由于对jQuery的了解不足,因此在转换方面遇到了困难。

Below is my conversion with jQuery lines commented out and followed by their most direct Javascript equivalent. 以下是我的转换,其中注释了jQuery行,其后是与它们最直接的Javascript对等项。 Keep in mind that some jQuery functions have different conversions depending on how you use them (though none of your code has this issue). 请记住,某些jQuery函数根据您的使用方式会有不同的转换(尽管您的代码都没有这个问题)。 Also, this code could benefit from a lot of cleanup both due to the conversion and due to issues with your own code. 另外,由于转换以及您自己的代码存在问题,因此可以从大量清理中受益。 I've avoided doing any cleanup in favor of demonstrating the jQuery/Javascript equivalency. 我避免进行任何清理工作以证明jQuery / Javascript的等效性。

 InitRadio('name'); function InitRadio(name) { val = 0; //$.each($(':radio[name="' + name + '"]'), function() { var radioButtons = [].slice.call(document.querySelectorAll('input[type="radio"][name="'+name+'"]')); radioButtons.forEach(function(element,index){ //$(this).val(val++); element.value = val++; //$(this).attr('chk', '0'); element.setAttribute('chk','0'); //$(this).on("click", function(event) { element.addEventListener('click',function(event){ //SetRadioButtonChkProperty($(this).val(), name); SetRadioButtonChkProperty(element.value, name); document.getElementById('1').innerText = document.getElementById('input1').getAttribute('chk'); document.getElementById('2').innerText = document.getElementById('input2').getAttribute('chk'); document.getElementById('3').innerText = document.getElementById('input3').getAttribute('chk'); }); }); document.getElementById('1').innerText = document.getElementById('input1').getAttribute('chk'); document.getElementById('2').innerText = document.getElementById('input2').getAttribute('chk'); document.getElementById('3').innerText = document.getElementById('input3').getAttribute('chk'); } function SetRadioButtonChkProperty(val, name) { //$.each($(':radio[name="' + name + '"]'), function() { var radioButtons = [].slice.call(document.querySelectorAll('input[type="radio"][name="'+name+'"]')); radioButtons.forEach(function(element,index){ //if ($(this).val() != val) if (element.value != val) //$(this).attr('chk', '0'); element.setAttribute('chk','0'); else { //if ($(this).attr('chk') == '0') if (element.getAttribute('chk') == '0') //$(this).attr('chk', '1'); element.setAttribute('chk','1'); } }); } 
 p { display: inline; } 
 <input type='radio' class='radio-button' name='name' id="input1"> <p id="1"></p> <input type='radio' class='radio-button' name='name' id="input2"> <p id="2"></p> <input type='radio' class='radio-button' name='name' id="input3"> <p id="3"></p> 

If you just want to know whether the radio button is checked, there's no need to set a custom attribute. 如果您只是想知道是否选中了单选按钮,则无需设置自定义属性。 Just look at its checked property. 只需查看其checked属性即可。

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

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