2011-07-21 42 views
0

我有一個表單,我想檢查用戶標識是否已被使用,然後在提交表單之前向用戶顯示答案。我找到了一種通過另一個站點來完成的方法,但是代碼返回時e.nodename是未定義的。我正在使用jQuery 1.6。有關如何做到這一點的任何想法?我對jQuery非常陌生。謝謝。獲取jquery在提交表單之前檢查用戶標識是否有效

+1

你是什麼意思*通過其他網站*? –

回答

0

讓之前假定你的提交按鈕

$.getJSON("http://host.com/?action=isUserIDavailable&userid=" + userid, function(){}); 

檢查,如果用戶標識可就是這樣的

<input type="submit" id="submit-button" name="submit" value="submit"/> 

你需要發送一個Ajax請求到用戶ID的服務器我猜你的用戶ID是類似於一個選擇菜單

<select id="user-id" name="user-id"> 
    <option value="1">xx</option> 
    ....etc 
</select> 

你的JavaScript代碼將是類似的東西

$(function(){ 
$('#submit-button').click(function(e){ 
    e.preventDefault(); // to prevent posting the form 
    //now sending ajax call to the server to check if the userID is valid 
    $.ajax({ 
    url: 'ajax.php', 
    data: {'userID':$('#user-id').val()} 
    success: function(data) { 
     if(data == "OK"){ 
     //the id is value 
     //you can post the form by using $('#form-id').submit(); 
     }else{ 
     //the id is n't valid 
     } 
    } 
    }); 

}); 
}); 
相關問題