当我选中另一个复选框时取消选中一个复选框 - laravel

uncheck a checkbox when I check another - laravel

我有一个问题,我需要在选中另一个复选框时取消选中一个复选框。但复选框不能有相同的名称。名称字段必须不同。

       <input type="radio" class="new-control-input" name="custom-radio-1">

  <input type="radio" class="new-control-input" name="custom-radio-2">

如何取消选中它们?

使用 JavaScript 你可以这样做:

function uncheckAndCheck(event) {

  // gets all radios with the name prefix like 'custom-radio-'
  // and uncheck all of them
  document.querySelectorAll( "input[type='radio'][name^='custom-radio-']" ).forEach( radio => {
    radio.checked = false;
  });
  
  // checks the radio that triggered the click event
  event.target.checked = true;
  
}
<input type="radio" class="new-control-input" name="custom-radio-1" onclick="uncheckAndCheck(event)"/>
<input type="radio" class="new-control-input" name="custom-radio-2" onclick="uncheckAndCheck(event)"/>