How to change form action url based on selected checkbox

南楼画角 提交于 2019-12-25 01:49:21

问题


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>

Thanks in advance


回答1:


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>


来源:https://stackoverflow.com/questions/27311850/how-to-change-form-action-url-based-on-selected-checkbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!