2014-12-05 52 views
0

我试图建立一个搜索框,学生可以根据课程代码或教职员姓名搜索课程储备。缺少的是可以根据您标记的复选框更改表单操作url的部分。我从之前的搜索框中将JavaScript代码用于下拉选择。我知道它不会对输入起作用,但这正是我的javascript理解结束的地方。顺便说一句,我有一个JavaScript运行,只允许选择一个复选框。我的代码是这样:如何根据选定的复选框更改表单动作网址

<form name="frm" method="get" action="http://path1" /> 
    <input name="SEARCH" value="" type="text" autocomplete="off" /> 
    <input type="submit" value="" /> 
    <ul> 
    <li> 
     <input type="checkbox" value="http://path1" checked="checked"/> 
     <label for="course">Course Code</label> 
    </li> 
    <li> 
     <input type="checkbox" value="http://path2"/> 
     <label for="faculty">Faculty Member</label> 
    </li> 
    </ul> 
</form> 
<script language="javascript"> 
var objForm = document.search; 
if (objForm.type.checked) 
    { 
    objForm.removeChild(objForm.searchType); 
    objForm.submit(); 
    } 
</script> 

在此先感谢

+1

哪里是JavaScript? – 2014-12-05 08:39:30

+0

对不起,添加了javascript。我也尝试** onchange =“document.frm.action = this.options [this.selectedValue] .value;”**但没有奏效。 Perhalps也可以用onchange来解决它。 – Hermes 2014-12-05 08:44:25

+0

你使用jQuery吗? – user1531437 2014-12-05 09:17:16

回答

0

既然你只有两个选择我已收音机,但仍然如果你想复选框,然后改变它:

<form name="frm" id="myForm" method="get" action="http://path1" > 
    <input name="SEARCH" value="" type="text" autocomplete="off" /> 
    <input type="submit" value="" /> 
    <ul> 
    <li> 
     <input type="radio" name="frmact" value="http://path1" checked="checked" onclick="formaction(this)"/> 
     <label for="course">Course Code</label> 
    </li> 
    <li> 
     <input type="radio" name="frmact" value="http://path2" onclick="formaction(this)"/> 
     <label for="faculty">Faculty Member</label> 
    </li> 
    </ul> 
</form> 
</body> 
<script> 
function formaction(checkbox){ 
    document.getElementById("myForm").action = checkbox.value;; 
} 

</script> 
+0

谢谢,它运作良好。 – Hermes 2014-12-05 09:43:47