当选中单选按钮时是否触发事件?

huangapple go评论65阅读模式
英文:

Is there an event triggered when a ratio button is checked?

问题

In JavaScript, 当单选按钮被选中时是否触发事件?可能的解决方案如下:

document.getElementById("radio1").addEventListener('change', () => {
	// 当第一个单选按钮被选中时执行某些操作
})

你可以使用 "change" 事件来监听单选按钮的选中状态变化。

英文:

In JavaScript, is there an event triggered when a radio button is checked?

<input type="radio" name="radio" id="radio1">
<input type="radio" name="radio" id="radio2" checked>
<input type="radio" name="radio" id="radio3">

Is something like the following possible?

document.getElementById("radio1").addEventListener('checked', () => {
	// Do something when the first radio button is checked
})

JSfidde: https://jsfiddle.net/Imabot/ko486uqe/2/

PLEASE read before answering or marking as duplicate. My question is not "How to run JS code when the radio button is checked". My question is "Is there an event triggered only when the radio button is checked", this is not exactly the same thing...

答案1

得分: 1

没有针对单选按钮的特殊事件。
没有针对*"radioChecked"*的特殊事件。

要测试checked状态,您需要自行处理。最好的方法是使用事件委托,检查元素目标是否为单选按钮元素,然后检查其checked属性:

addEventListener(`input`, (evt) => {
  const elRadio = evt.target.closest(`[type="radio"]`);
  if (!elRadio) return; // 不是单选按钮元素。什么也不做
  
  console.log(`${elRadio.id} ${elRadio.checked}`);
});

虽然始终使用Event.target.closest()是一个好习惯,但在处理input等动作元素时,您也可以使用Element.matches()方法:

addEventListener(`input`, (evt) => {
  if (!evt.target.matches(`[type="radio"]`)) return; // 什么也不做
  
  console.log(`${evt.target.id} ${evt.target.checked}`);
});
英文:

There's no such special event for radio buttons only.
There's not such special event for "radioChecked".

To test the checked state you need to do it on your own.
The best would be to use event delegation, check if the element target is a Radio Element, and then go for its cehecked property:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

addEventListener(`input`, (evt) =&gt; {
  const elRadio = evt.target.closest(`[type=&quot;radio&quot;]`);
  if (!elRadio) return; // Not a radio element. Do nothing
  
  console.log(`${elRadio.id} ${elRadio.checked}`); 
});

<!-- language: lang-html -->

&lt;input type=&quot;radio&quot; name=&quot;radio-group-one&quot; id=&quot;radio-1&quot;&gt;
&lt;input type=&quot;radio&quot; name=&quot;radio-group-one&quot; id=&quot;radio-2&quot; checked&gt;
&lt;input type=&quot;radio&quot; name=&quot;radio-group-one&quot; id=&quot;radio-3&quot;&gt;

<!-- end snippet -->

Whilst using Event.target.closest() is always a good practice, you can also use the Element.matches() method when handling actionElements like input:

addEventListener(`input`, (evt) =&gt; {
  if (!evt.target.matches(`[type=&quot;radio&quot;]`)) return; // Do nothing
      
  console.log(`${evt.target.id} ${evt.target.checked}`); 
});

答案2

得分: -1

尝试这样做:

    document.getElementById("radio1").addEventListener('change', () => {
        if (document.getElementById("radio1").checked) {
            // 当第一个单选按钮被选中时执行某些操作
            console.log("radio1被选中");
        }
    });
英文:

Try this:

document.getElementById(&quot;radio1&quot;).addEventListener(&#39;change&#39;, () =&gt; {
    if (document.getElementById(&quot;radio1&quot;).checked) {
        // Do something when the first radio button is checked
	    console.log(&quot;radio1 checked&quot;);
    }
});

huangapple
  • 本文由 发表于 2023年5月21日 17:49:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76299266.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定