2013-06-18 63 views
0

我有兩個表單在頁面上提交都做同樣的事情。兩個表格在同一頁上提交,只有第一個工作。 JQuery

一個在工具欄/導航欄上,另一個在主頁面上。
我遇到的問題是,一旦我在工具欄上添加表單時,主頁面上的表單在觸發時從不具有搜索值,如果我在工具欄中註釋掉該表單,那麼主頁面就具有該值。

//Toolbar submit 
<form> 
    <div class="input-append"> 
    <input type="text" class="span3" placeholder="Enter name" /> 
    <button type="submit" class="btn-u">Go</button> 
    </div> 
</form> 

// Main page submit 
<form class="form-inline margin-bottom-5"> 
    <div class="input-append"> 
    <input type="text" class="input-xxlarge" placeholder="Enter class name"> 
    <button type="submit" class="btn-u">Go</button> 
    </div> 
</form> 

//JavaScript/JQuery 
$("form").submit(function (e) { 

    var searchTerm = $("input:first").val(); 
    if (searchTerm === '') { 
    // This part of the code is reached for the second submit, 
    // I have tried using id on the submit but have the same result 
    } 

回答

3

更改如下,它在尋找頁面上的第一個輸入的時刻,這將尋找這實際上已提交表單中的第一個輸入。

var searchTerm = $("input:first").val(); 

var searchTerm = $(this).find("input:first").val(); 
+0

感謝瑞安的作品.. –

1

您正在尋找var searchTerm = $("input:first").val();沒有告訴你正在尋找在提交表單的第一個輸入。在這裏,您正在尋找頁面第一種形式的第一個輸入。取而代之的是var searchTerm = $(this).find("input:first").val();

編輯:嘿,我太慢了回答...至少它證實瞭解決方案。

1

使用「本」,以確保您正在尋找的形式,你提交的第一個輸入:

$("form").submit(function (e) { 

    var searchTerm = $(this).find("input:first").val(); 
    if (searchTerm === '') { 
     // This part of the code is reached for the second submit, 
     // I have tried using id on the submit but have the same result 
    } 
相關問題