我有一个使用jQuery和ajaxForm插件提交的登录表单。表单提交正常,我收到了JSON响应aok:
{"username":"Enter your username","password":"password must not be empty"}
我的表格如下使用kohana框架:
<?php echo form::open('login',array('id'=>'login'));?>
<div id="username_errors"></div>
<?php echo form::label('username','Username');?><br/>
<?php echo form::input('username','',array('id' => 'username','class'=>'login text ui-widget-content ui-corner-all'));?><br/>
<div id="password_errors"></div>
<?php echo form::label('password','Password');?><br/>
<?php echo form::password('password','',array('id' => 'password','class'=>'text ui-widget-content ui-corner-all'));?><br/>
<?php echo form::label('remember_me','Remember Me');
echo form::checkbox('Remember Me', 'remember_me', FALSE);?><br/>
<?php echo form::submit('submit','Login');
echo form::close();?>
我的jQuery:
jQuery('.form #login').ajaxForm({
data: jQuery(this).serialize(),
dataType:'json',
url:'login',
success: function(data){
if (data.redirect == 'home') {
window.location.href=data.redirect;
}//else append error messages to relevant divs
}
});
我想收集我的错误消息(如果有的话)并使用错误消息填充相关的div。有什么想法吗?
答案 0 :(得分:1)
我认为你非常接近,试试:
jQuery('.form #login').ajaxForm({
data: jQuery(this).serialize(),
dataType:'json',
url:'login',
success: function(data){
if (data.redirect == 'home') {
window.location.href=data.redirect;
} else {
$('#username_errors').html(data.username);
$('#password_errors').html(data.password);
}
});