我试图找出如何解决这个问题:
$('#username').blur(function(){ $.post('register/isUsernameAvailable',{"username":$('#username').val()},function(data){ if(data.username == "found"){ alert('username already in use'); } },'json'); });
接近这个:
rules: { username: { minlength: 6,maxlength: 12,remote: { url: 'register/isUsernameAvailable',type: 'post',data: { 'username': $('#username').val() } } }
但是我很难完成它.我想要的是代替警报,让它显示错误消息,但我可以在实际的jquery验证消息中设置消息.
http://docs.jquery.com/Plugins/Validation/Methods/remote#options
更新:
出于某种原因,它没有把它作为一个POST做它作为GET请求并且不确定原因.这是更新的代码:
rules: { username: { minlength: 6,dataType: 'post',data: { 'username': $('#username').val() },success: function(data) { if (data.username == 'found') { message: { username: 'The username is already in use!' } } } } },
更新2:
现在,我正在回到某个地方,我正在收到POST请求.我还有两个问题.其中一个事实是,要完成另一个POST请求,用户必须刷新表单.最后一个问题是,如果找到返回的用户名,则不会显示错误消息.
rules: { username: { minlength: 6,remote: { type: 'post',url: 'register/isUsernameAvailable',dataType: 'json',
更新:
public function isUsernameAvailable() { if ($this->usersmodel->isUsernameAvailable($this->input->post('username'))) { return false; } else { return true; } }
更新4:
控制器:
public function isUsernameAvailable() { if ($this->usersmodel->isUsernameAvailable($this->input->post('username'))) { return false; } else { return true; } } public function isEmailAvailable() { if ($this->usersmodel->isEmailAvailable($this->input->post('emailAddress'))) { return false; } else { return true; } }
模型:
/** * Check if username available for registering * * @param string * @return bool */ function isUsernameAvailable($username) { $this->db->select('username'); $this->db->where('LOWER(username)=',strtolower($username)); $query = $this->db->get($this->usersTable); if ($query->num_rows() == 0) { return true; } else { return false; } } /** * Check if email available for registering * * @param string * @return bool */ function isEmailAvailable($email) { $this->db->select('email'); $this->db->where('LOWER(email)=',strtolower($email)); $query = $this->db->get($this->usersTable); if($query->num_rows() == 0) { return true; } else { return false; } }@H_403_38@
解决方法
好吧,我特别关注你的插件概念,但是如果用户名大于6或小于12,我希望它检查一下这个插件.从你的jQuery没有插件的核心示例中,它就像添加一样简单一个小的if – 否则这个概念.
$('#username').blur(function(){ if($('#username').val().length < 6 || $('#username').val().length > 12) { alert('Username must be between 6 and 12 characters'); } else { $.post('register/isUsernameAvailable',function(data){ if(data.username == "found"){ alert('username already in use'); } },'json'); } });@H_403_38@ @H_403_38@ 原文链接:https://www.f2er.com/jquery/181310.html