2011-03-15 44 views
1
<input type ="checkbox" id = "checkbox1" checked ="checked" value="1" onclick = "a()"/> 
<input type ="checkbox" id = "checkbox1" checked ="checked" value="2" onclick = "a()"/> 

function(checkbox1,checkbox2) 
{ 
    if(checkbox1.checked == true && checkbox2.checked == false) 
    { 
    alert("checkbox1 is checked, Checkbox2 ix Unchecked"); 
    } 
} 

如何通過checkbox1和checkbox2中的值?將2個不同的複選框中的2個參數合併爲一個函數

回答

2

您可以直接用搶吧:

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); 
} 
0

下面是獲取所有複選框的形式並顯示它們的值和狀態

<form id="myForm"> 
<input type ="checkbox" id = "checkbox1" checked ="checked" value="1" onclick = "a()"/> 
<input type ="checkbox" id = "checkbox2" checked ="checked" value="2" onclick = "a()"/> 
</form> 
<script> 
function a(){ 
    form = document.getElementById("myForm") 
    str = "" 
for (i=0;i<form.elements.length;i++) { 
    type = form.elements[i].type 
    if (type=="checkbox") { 
      str += "\n" 
     str += (form.elements[i].checked)? form.elements[i].id + " is checked " : form.elements[i].id + " is unchecked "; 
     str += "\n" 
     str += form.elements[i].id + " value is "+ form.elements[i].value; 
    } 


} 
    alert(str); 
} 
</script> 
的溶液

如果你想只是個別值/狀態見下文 對於價值

<input type ="checkbox" id = "checkbox1" checked ="checked" value="1" onclick = "a(this.value)"/> 
    <input type ="checkbox" id = "checkbox1" checked ="checked" value="2" onclick = "a(this.value)"/> 
    <script> 
    function a(v){ 
     alert(v) 
    } 
    </script> 

對於選中/取消選中狀態

<input type ="checkbox" id = "checkbox1" checked ="checked" value="1" onclick = "a(this.checked)"/> 
<input type ="checkbox" id = "checkbox1" checked ="checked" value="2" onclick = "a(this.checked)"/> 
<script> 
function a(v){ 
    alert(v) 
} 
</script> 
+0

這將是值,而不是檢查狀態。 – alex 2011-03-15 03:15:38

+0

問題狀態「我怎樣才能通過複選框值」 – Michal 2011-03-15 03:17:32

+1

其實我不確定他現在要求什麼,我認爲他想做他列出的功能,並通過實際的checkbox1和checkbox2進入。 – kjy112 2011-03-15 03:19:26

0

我想你可以做這種方式...如果你真的想要

onclick="a(document.getElementById('checkbox1'), document.getElementById('checkbox2'))" 
相關問題