2011-12-14 91 views
2

我正嘗試將表單提交給表單提交上的2個不同腳本。 ScriptA提交表單以提交重定向。 ScriptB不會給出任何響應,只需將給定的信息寫下來即可。表單提交上的JQuery多個POST

我無法得到提交的工作,我對JQuery不是很有經驗,這是我第一次,所以我希望有人能指出爲什麼我的表單不能正確提交與JQUERY。

的HTML正文:

<script type="text/javascript"> 
$(document).ready(function(){ 
    $('#submit').click(function(){ 
    //Send data to the email script 
    $.post('authenticate.php', $('Auth').serialize(), function(data, textStatus) { 
     //data is the result from the script 
     alert(data); 
    }); 

    //Send data to the other script 
    $.post('http://Service3.com/j_security_check', $('Auth').serialize(), function(data, textStatus) { 
     //data is the result from the script 
     alert(data); 
    }); 
    }); 
}); 
</script> 

HTML表單的主體:

<form action=authenticate.php method=post name=Auth id="form" class="appnitro"> 
<div class="form_description"><h2>Login</h2></div> 
<ul> 
    <li id="li_1" > 
     <label class="description" for="element_1">Username </label> 
     <div> 
     <input id="element_1" name="j_username" class="element text medium" type="text" maxlength="255" value=""/> 
     </div> 
    </li> 

    <li id="li_2" > 
     <label class="description" for="element_2">Password </label> 
     <div> 
     <input id="element_2" name="j_password" class="element text medium" type="password" maxlength="255" value=""/> 
     </div> 
    </li> 

    <li class="buttons"> 
    <input id="saveForm" class="button_text" type="submit" name="submit" id="submit" value="Log In" /> 
    </li> 
</ul> 
</form> 

一個題外話的問題我已經是我怎麼修復我的代碼的格式?每當我在這裏發佈我的代碼時,我總會得到巨大的縮進。

+1

RE格式:使用空格代替製表符。 4個空格= 4個空格,但1個標籤= 8個空格。此外,使用您的編輯器SHIFT +標記整個代碼,直到最左邊的行在發佈前只有1個選項卡。一旦你複製它,你可以將它重新放回原處。您的編輯器還應具有清理整個文檔中的縮進的功能,以免在縮進行中出現多餘的製表符。希望這有助於:) – mellamokb 2011-12-14 18:30:03

回答

3
$(document).ready(function(){ 

    $('#form').submit(function(){ 

    $.ajax({ 
     type: 'POST', 
     async: false, 
     url: "your_url_1", 
     data: $(this).serialize(), 
     success: function(data, status, xhr){ 
     alert('ok'); 
     }, 
     error: function(xhr, status, err) { 
     alert(status + ": " + err); 
     } 
    }); 

    $.ajax({ 
     type: 'POST', 
     async: false, 
     url: "your_url_2", 
     data: $(this).serialize(), 
     success: function(data, status, xhr){ 
     alert('ok'); 
     }, 
     error: function(xhr, status, err) { 
     alert(status + ": " + err); 
     } 
    }); 

    }); 

}); 
1

您的代碼存在一些問題。爲了不綁定到提交按鈕的click事件,爲什麼不綁定到表單的submit事件。此外,當您序列化您想要定位表格的表格時,您正在選擇$('Auth'),它將嘗試選擇任何Auth標記(不存在)。

$('#form').submit(function(){ 
    //Send data to the email script 
    $.post('authenticate.php', $(this).serialize(), function(data, textStatus) { 
     //data is the result from the script 
     alert(data); 
    }); 
    return false;//this stops the form from submitting normally 
}); 

你的第二個$.post()看起來像它的發送形式提交給了一個用戶當前在不同的領域。您只能JSONP做到這一點:http://api.jquery.com/jquery.ajax(做JSONP搜索,你會發現關於如何做到這一點優秀的信息)

+0

我的表單的名稱標籤是Auth,name = Auth,所以我認爲它會佔用表單中的所有字段並POST它們。 – Bulvak 2011-12-14 18:33:21

+0

@Nadal要按名稱選擇元素,請使用以下語法:`$('[name =「Auth」]')`。 `name`是一個屬性,所以我們使用方括號來按屬性進行搜索。 http://api.jquery.com/attribute-equals-selector/ – Jasper 2011-12-14 18:34:59