简体   繁体   English

如何创建一个表单来生成URL

[英]how to create a form to generate url

i have a form on my site to generate url and it works well but the user should wait until the whole page is loaded to be able to use it properly , i think that happens because i use window.onload and i should use $(document).ready instead of it , but i couldnt replace it i need someone to help me with it 我在我的网站上有一个表单可以生成url,并且效果很好,但是用户应该等到整个页面加载完毕后才能正确使用它,我认为这是因为我使用window.onload并且应该使用$(document ).ready代替它,但是我无法替换它,我需要有人来帮助我

<script type='text/javascript'>
   window.onload = function() {
   document.forms['filterform'].onsubmit = filterLoad;
};

function filterLoad() {
  var url,
      colourSel = this.elements['department'],
      shapeSel = this.elements['country'];

  url = ( colourSel.options[colourSel.selectedIndex].value ) + ( shapeSel.options[shapeSel.selectedIndex].value ); 
 window.location.href = url;
 return false;
}

</script>




<form   id="searchform" name="filterform">
   <select class="chosen" name="department" id="searchfield" >
      <option value="http://www.mysite.com/">choise 1</option>
      <option value="http://www.mysite.com/">choise 1</option>
   </select>

   <select class="chosen" name="country" id="choose">
      <option value="?qatar=show&action=Filter">qatar</option>
      <option value="?ksa=show&action=Filter">ksa</option>
   </select>

   <label for="search"><button  class="submit" onclick="filterLoad()"  /></button></label>

</form>

我会摆脱所有额外的JavaScript来分配功能,而只需使用:

<form   id="searchform" name="filterform" onsubmit="filterLoad">

You have the right idea, I would just call that function though when the form is submitted, and remove the on click event from the submit button. 您有正确的想法,尽管在提交表单时我只是调用该函数,然后从“提交”按钮中删除onclick事件。

Try this : 尝试这个 :

<script type='text/javascript'>

function filterLoad() {
  var url,
      colourSel = this.elements['department'],
      shapeSel = this.elements['country'];

      url = (colourSel.options[colourSel.selectedIndex].value) + (shapeSel.options[shapeSel.selectedIndex].value);
      window.location.href = url;
      return false;
}
</script>

<form id="searchform" name="filterform" onSubmit="return filterLoad();" >
   <select class="chosen" name="department" id="searchfield" >
      <option value="http://www.mysite.com/">choise 1</option>
      <option value="http://www.mysite.com/">choise 1</option>
   </select>

   <select class="chosen" name="country" id="choose">
      <option value="?qatar=show&action=Filter">qatar</option>
      <option value="?ksa=show&action=Filter">ksa</option>
   </select>
   <label for="search"><button  class="submit" /></button></label>
</form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM