Passing 2 Parameter In 2 Different Checkbox Into A Function
<input type ="checkbox" id = "checkbox1" checked ="checked" value="1" onclick = "a()"/>
<input type ="checkbox" id = "checkbox1" checked ="check开发者_如何学运维ed" value="2" onclick = "a()"/>
function(checkbox1,checkbox2)
{
if(checkbox1.checked == true && checkbox2.checked == false)
{
alert("checkbox1 is checked, Checkbox2 ix Unchecked");
}
}
how can i pass the checkbox1 and checkbox2 value in a()???
You can just grab it using :
var mycheckbox1 = document.getElementById('checkbox1');
and
var mycheckbox2 = document.getElementById('checkbox2');
You might want to change your checkbox2's id to id='checkbox2' having non-unique id is not valid syntax.
If you were asking to modify the function you posted then it's the following:
<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');
}
Now if you were asking to get the value of the checkbox that is checked then it's the following:
<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);
}
I suppose you could do it this way... If you really wanted to
onclick="a(document.getElementById('checkbox1'), document.getElementById('checkbox2'))"
Here is a solution that gets all checkboxes in the form and displays their value and state
<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>
If you want just individual values /state see below For value
<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>
For checked/unchecked state
<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>
精彩评论