0

我的网站上有一个表单来生成 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>
4

2 回答 2

1

我会摆脱所有额外的 javascript 来分配函数并使用:

<form   id="searchform" name="filterform" onsubmit="filterLoad">
于 2013-10-09T21:41:51.747 回答
1

你有正确的想法,我只是在提交表单时调用该函数,并从提交按钮中删除点击事件。

尝试这个 :

<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>
于 2013-10-09T21:46:58.440 回答