Javascript,选中一个复选框=location.html,选中两个复选框=另一个location.html
我试图用html和javascript创建的表单有问题 首先,我想有多个复选框 如果选中了一个,则onsubmit会将用户带到一个url 如果选中了两个框,则提交时会将用户带到另一个url等Javascript,选中一个复选框=location.html,选中两个复选框=另一个location.html,javascript,jquery,Javascript,Jquery,我试图用html和javascript创建的表单有问题 首先,我想有多个复选框 如果选中了一个,则onsubmit会将用户带到一个url 如果选中了两个框,则提交时会将用户带到另一个url等 这可能吗?如果可能的话,javascript或jquery会是什么?有些代码会很好,因为我不知道您提交表单时使用的是什么机制,但解决方案很可能涉及如下内容: switch ($('form input:checkbox:checked').length) { case 1:
这可能吗?如果可能的话,javascript或jquery会是什么?有些代码会很好,因为我不知道您提交表单时使用的是什么机制,但解决方案很可能涉及如下内容:
switch ($('form input:checkbox:checked').length)
{
case 1:
window.location.href = url1;
break;
case 2:
window.location.href = url2;
break;
//etc
default:
}
编辑:好的,我必须给瓦伦丁一些信任,所以让我们结合解决方案
var $myForm = document.getElementById('myForm');
$('#myForm input:checkbox').on('change', function(event){
switch ($('#myForm input:checkbox:checked').length)
{
case 1:
$myForm.action = url1;
break;
case 2:
$myForm.action = url2;
break;
//etc
default:
}
});
我认为您可以使用复选框的
onCheck
和onUnCheck
,让他们将表单的action属性修改为用户应该使用的任何页面
因此,您的HTML可以是:
<form method="post" action="whateverpage.php" id="myform">
<input type="checkbox" id="check0"/>
<input type="checkbox" id="check1"/>
<input type="submit" value="Submit" />
</form>
当然,您可以使用
yourform.setAttribute(“action”,“page.php”)代码>或者。请发布您的代码或您尝试的任何内容。到目前为止,你在代码方面有什么进展?在提交时运行代码是否有问题?是选择发送到哪个页面,等等。
yourform=document.getElementById("myform");
checkbox0=document.getElementById("check0");
checkbox1=document.getElementById("check1");
checkbox0.onCheck=function(){
if(checkbox1.checked==true){
yourform.action="bothchecked.php";
}else{
yourform.action="firstchecked.php";
}
}
checkbox1.onCheck=function(){
if(checkbox0.checked==true){
yourform.action="bothchecked.php";
}else{
yourform.action="secondchecked.php";
}
}
checkbox1.onUnCheck=function(){
if(checkbox0.checked==true){
yourform.action="firstchecked.php";
}else{
yourform.action="nonechecked.php";
}
}
checkbox0.onUnCheck=function(){
if(checkbox1.checked==true){
yourform.action="secondchecked.php";
}else{
yourform.action="nonechecked.php";
}
}