在提交表单之前获取jquery来检查用户标识是否有效

时间:2011-07-21 18:44:34

标签: javascript jquery ajax

我有一个表单,我想检查是否已经使用了userID,然后在提交表单之前显示用户的答案。我找到了通过另一个站点完成此操作的方法,但代码返回时e.nodename未定义。我正在使用jQuery 1.6。有关如何做到这一点的任何想法?我是jQuery的新手。感谢。

2 个答案:

答案 0 :(得分:1)

我会做一个

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

在提交表单

之前检查用户标识是否可用

答案 1 :(得分:0)

我们假设你的提交按钮是这样的

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

您需要使用userID向服务器发送ajax请求我猜您的userID类似于选择菜单

<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
      }
    }
  });

 });
});