简体   繁体   English

在 html 中只使一个复选框互斥

[英]Make only one checkbox mutually exclusive in html

I am looking to create a list of checkboxes that a user can select and at the bottom of the list a separate checkbox that is labeled as none so that when a user selects that checkbox, all the other checkboxes are unticked.我希望创建一个用户可以使用 select 的复选框列表,并在列表底部有一个单独的复选框,标记为无,这样当用户选择该复选框时,所有其他复选框都不会被选中。 Additionally, when the none checkbox is selected and one of the other options is selected, the none checkbox gets deselected. Additionally, when the none checkbox is selected and one of the other options is selected, the none checkbox gets deselected.

Fairly straightforward;非常坦率的; add change events to everything and just uncheck as necessarychange事件添加到所有内容,并根据需要取消选中

 var cbs = document.querySelectorAll(".option") var none = document.querySelector(".none") cbs.forEach(cb => { cb.addEventListener("change", () => { if(cb.checked) none.checked = false }) }) none.addEventListener("change", () => { if(none.checked) { cbs.forEach(cb => cb.checked = false); } })
 <div><input type="checkbox" class="option">1</div> <div><input type="checkbox" class="option">2</div> <div><input type="checkbox" class="option">3</div> <div><input type="checkbox" class="option">4</div> <hr> <div><input type="checkbox" class="none">none</div>

this way..这边走..

myForm.oninput = e => {
  if (e.target === myForm.none && myForm.none.checked )
    myForm.querySelectorAll('input[type=radio]').forEach(r=>r.checked=false) 
  else if (e.target.matches('input[type=radio]') && e.target.checked)
    myForm.none.checked = false 
  }

demo:演示:

 const myForm = document.forms['my-form'] myForm.oninput = e => { if (e.target === myForm.none && myForm.none.checked ) { myForm.querySelectorAll('input[type=radio]').forEach(r=>r.checked=false) } else if (e.target.matches('input[type=radio]') && e.target.checked) { myForm.none.checked = false } } myForm.onsubmit = e => { e.preventDefault() // disable submit (page reload) console.clear() let formInputs = Object.fromEntries( new FormData(myForm).entries() ) console.log( formInputs ) }
 body { font-family: Helvetica, Arial sans-serif; font-size: 12px; } form { width: 22em; } fieldset { margin: .7em;}.as-console-wrapper { max-height:100%;important: top;0: left;50%:important; width.50%: };as-console-row { background-color. yellow: }:as-console-row:;after { display:none !important; }
 <form name="my-form"> <label><input type="checkbox" name="none"> none </label> <fieldset> <legend>group A</legend> <label><input type="radio" name="radioA" value="a1"> A 1</label> <label><input type="radio" name="radioA" value="a2"> A 2</label> <label><input type="radio" name="radioA" value="a3"> A 3</label> <label><input type="radio" name="radioA" value="a4"> A 4</label> </fieldset> <fieldset> <legend>group B</legend> <label><input type="radio" name="radioB" value="b1"> B 1</label> <label><input type="radio" name="radioB" value="b2"> B 2</label> <label><input type="radio" name="radioB" value="b3"> B 3</label> <label><input type="radio" name="radioB" value="b4"> B 4</label> </fieldset> <fieldset> <legend>group C</legend> <label><input type="radio" name="radioC" value="c1"> C 1</label> <label><input type="radio" name="radioC" value="c2"> C 2</label> <label><input type="radio" name="radioC" value="c3"> C 3</label> <label><input type="radio" name="radioC" value="c4"> C 4</label> </fieldset> <button type="submit">submit</button> </form>

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

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