单击另一组复选框中的选项时,一组复选框中的选项必须保留为默认值

我有两组复选框。当我选择带有 id radio14 的复选框(在第一个中)时,我希望自动选中带有 id radio12 的复选框作为默认值。


var chk1 = document.getElementById("#radio14");

var chk2 = document.getElementById("#radio12");



chk1.onclick = function(){

  if( chk1.checked == 1 ) {

    chk2.setAttribute('checked', true);

  } else {

    chk2.setAttribute('checked', false);

  }

};

<input type="radio" name="Finishing" id="radio12" class="radio"/>

<label for="radio12">Silky Matt lamination</label>


<input type="radio" name="Special_Finishing" id="radio14" class="radio"/> 

<label for="radio14">Digital Embossing </label>


米脂
浏览 154回答 1
1回答

德玛西亚99

试试这个:const checkbox1 = document.getElementById('radio14');const checkbox2 = document.getElementById('radio12');checkbox1.addEventListener('change', () => {&nbsp; if (checkbox1.checked) {&nbsp; &nbsp; checkbox2.setAttribute('checked', true);&nbsp; } else {&nbsp; &nbsp; checkbox2.setAttribute('checked', false);&nbsp; }});<input type="radio" id="radio12" /><label for="radio12">Silky Matt lamination</label><input type="radio" id="radio14"/>&nbsp;<label for="radio14">Digital Embossing</label>如果您正在使用getElementById,则只需通过id,无需#.
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript