我有两组复选框。当我选择带有 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>
1 回答

德玛西亚99
TA贡献1770条经验 获得超3个赞
试试这个:
const checkbox1 = document.getElementById('radio14');
const checkbox2 = document.getElementById('radio12');
checkbox1.addEventListener('change', () => {
if (checkbox1.checked) {
checkbox2.setAttribute('checked', true);
} else {
checkbox2.setAttribute('checked', false);
}
});
<input type="radio" id="radio12" />
<label for="radio12">Silky Matt lamination</label>
<input type="radio" id="radio14"/>
<label for="radio14">Digital Embossing</label>
如果您正在使用getElementById
,则只需通过id
,无需#
.
添加回答
举报
0/150
提交
取消