简体   繁体   English

如何将单选按钮值设置为选中或未选中的情况

[英]How to set radio button value as checked or unchecked situation

<input type="radio" name="imgsel"  value="" checked />

My requirement is: This radio button by default checked then value of this button is 'present'.我的要求是:默认选中此单选按钮,然后此按钮的值为“存在”。 When this button is unchecked, then value this button is 'absent'.如果未选中此按钮,则该按钮的值为“不存在”。 how can i do this?我怎样才能做到这一点?

please, help me.请帮我。 Thanks in advance.提前致谢。

 function valChange(element) { if (element.checked) { alert("present"); } else { alert("absent"); } }
 <input id="button" type="checkbox" name="imgsel" onchange="valChange(this)" value="" checked />

The following example replaces the value of the checkbox when clicked.以下示例在单击时替换复选框的值。 First the click event must be bound to the checkbox.首先,点击事件必须绑定到复选框。 The EventHandler checks whether the box has already been checked. EventHandler 检查该框是否已被选中。 If not, it checks it and sets the value in the checkbox.如果没有,它会检查它并在复选框中设置值。 And if it has not been checked, the other way round.如果它没有被检查,反之亦然。

 let i = document.querySelector('input'); i.addEventListener('click', (ev) => { let el = ev.target; if (el.getAttribute('checked') === null) { el.setAttribute('checked', true); el.value = "present" } else { el.removeAttribute('checked'); el.value = "absent" } let checkValue = document.querySelector('input').value alert(checkValue) })
 <input type="radio" name="imgsel" value="" checked />

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

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