Javascript 将2个不同复选框中的2个参数传递到函数中 功能(复选框1、复选框2) { if(checkbox1.checked==true&&checkbox2.checked==false) { 警报(“复选框1已选中,复选框2未选中”); } }

Javascript 将2个不同复选框中的2个参数传递到函数中 功能(复选框1、复选框2) { if(checkbox1.checked==true&&checkbox2.checked==false) { 警报(“复选框1已选中,复选框2未选中”); } },javascript,html,Javascript,Html,如何在a()中传递checkbox1和checkbox2值?您可以使用以下方法获取它: <input type ="checkbox" id = "checkbox1" checked ="checked" value="1" onclick = "a()"/> <input type ="checkbox" id = "checkbox1" checked ="checked" value="2" onclick = "a()"/> function(checkbox

如何在a()中传递checkbox1和checkbox2值?

您可以使用以下方法获取它:

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

您可能希望将checkbox2的id更改为id='checkbox2',但id不唯一是无效的语法

如果您要求修改您发布的功能,则如下所示:

var mycheckbox2 = document.getElementById('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');
}

函数a(){
var mycheckbox1=document.getElementById('checkbox1');
var mycheckbox2=document.getElementById('checkbox2');
如果(mycheckbox1.checked&!checkbox2.checked)
警报('复选框1已选中,复选框2未选中');
}
现在,如果您要求获取选中复选框的值,则如下所示:

var mycheckbox2 = document.getElementById('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');
}

功能a(框){
如果(复选框)
警报(框值);
}

这是一个解决方案,它获取表单中的所有复选框,并显示它们的值和状态

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

函数a(){
form=document.getElementById(“myForm”)
str=“”
对于(i=0;i我想你可以这样做……如果你真的想这样做的话

<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>

这将是值,而不是选中状态。问题是“我如何传递复选框值”实际上我不确定他现在要求的是什么,我以为他想执行他列出的功能,并传递实际的复选框1和复选框2。
<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>
onclick="a(document.getElementById('checkbox1'), document.getElementById('checkbox2'))"