您可以直接用搶吧:
var mycheckbox1 = document.getElementById('checkbox1');
和
var mycheckbox2 = document.getElementById('checkbox2');
您可能希望將checkbox2的ID改變成非唯一的ID = 'checkbox2' 不是有效的語法。
如果你問的修改,然後您發佈的功能,它的下面:
<input type ="checkbox" id = "checkbox1" checked ="checked" value="1" onclick = "a()"/>
<input type ="checkbox" id = "checkbox2" checked ="checked" value="2" onclick = "a()"/>
function a(){
var mycheckbox1 = document.getElementById('checkbox1');
var mycheckbox2 = document.getElementById('checkbox2');
if(mycheckbox1.checked && !checkbox2.checked)
alert('checkbox1 is checked, checkbox2 is unchecked');
}
現在如果你問來獲取被選中,則複選框的值是以下幾點:
<input type ="checkbox" id = "checkbox1" checked ="checked" value="1" onclick = "a(this)"/>
<input type ="checkbox" id = "checkbox2" checked ="checked" value="2" onclick = "a(this)"/>
function a(box){
if(box.checked)
alert(box.value);
}
這將是值,而不是檢查狀態。 – alex 2011-03-15 03:15:38
問題狀態「我怎樣才能通過複選框值」 – Michal 2011-03-15 03:17:32
其實我不確定他現在要求什麼,我認爲他想做他列出的功能,並通過實際的checkbox1和checkbox2進入。 – kjy112 2011-03-15 03:19:26