2013-10-09 42 views
0

我在我的網站上有一個表單來生成url,它運行良好,但用戶應該等到整個頁面加載才能正確使用它,我認爲這是因爲我使用在window.onload和我應該使用$(文件)。就緒,而不是它,但我不能代替它,我需要有人來幫我一下吧如何創建一個表單來生成url

<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> 

回答

1

我會擺脫所有額外的JavaScript來分配功能和用途:

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

你有正確的想法,我只是調用該功能,雖然w當表單被提交時,並從提交按鈕中刪除點擊事件。

試試這個:

<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> 
+0

謝謝你,但是當我刪除的功能分配給我的形式額外jvascript這是行不通的,它不能通過onsubmit屬性 – user1921704

+0

修補它調用filterload() ,給一個鏡頭 – X3074861X

相關問題