2011-07-21 98 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 
     } 
    } 
    }); 

}); 
}); 
相关问题