我有两组复选框。当我选中带有 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>
试试这个:
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
,而不能传递#
。