如何基于选中的复选框更改表单操作URL [英] How to change form action url based on selected checkbox
问题描述
我正在尝试建立一个搜索框,让学生可以根据他们的课程代码或教职员工姓名搜索课程储备.缺少的是可以根据所标记的复选框更改表单操作URL的部分.我从上一个搜索框中获取了JavaScript代码,该代码曾在下拉菜单中使用过.我知道它对输入不起作用,但这就是我对JavaScript的理解结束的地方.顺便说一句,我有一个正在运行的javascript,它只允许选择一个复选框.我的代码如下:
I'm trying to build a search box were students can search for course reserves based on their course code or faculty members names. What is missing is the part where you can change the form action url based on the checkbox you mark. I took the javascript code from the previous search box where it was used on a dropdown selection. I knew it wouldn't work on input but that's where my javascript understanding ends. Btw, I have a javascript running which allows only one checkbox to be selected. My code goes like:
<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>
预先感谢
推荐答案
由于只有两个选项,因此我将其设置为单选,但如果仍要选中该复选框,则将其更改:
Since you have only two option I have made it radio but still if you want checkbox then change it:
<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>
这篇关于如何基于选中的复选框更改表单操作URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!