多形式的页面 - 通过AJAX加载错误信息 [英] Multiple forms in in a page - load error messages via ajax
问题描述
我有多种形式在通过jQuery AJAX一个页面提交。所有表格使用相同的类名。另外,我使用malsup jQuery插件的形式提交。 我可以将数据成功地传递到PHP文件,但该错误信息是反映各种形式的div.message。我不希望有不同的形式的ID,因为我需要在jQuery的所有ID即可。
I having multiple forms in a single page submitting via jQuery ajax. All forms use same class names. Also i am using malsup jquery plugin for form submission. I can successfully pass the data to a php file but the error message is reflecting on all forms div.message. I don't want to have different form ids because i need to have all ids in the jquery then.
<form class="myform" method="post" action="process.php">
<input type="text" value="" >
<input type="submit" value="Submit" >
<div class="message"></div>
</form>
<form class="myform" method="post" action="process.php">
<input type="text" value="" >
<input type="submit" value="Submit" >
<div class="message"></div>
</form>
jQuery的一部分
jQuery part
<script>
$(document).ready(function() {
$('.myform').on('submit', function(event) {
event.preventDefault();
$(this).ajaxSubmit({
url: 'process.php',
type: 'post',
dataType: 'json',
success: function( response ){
$.each(response, function(){
$('div.message').append('<div>'+message+'</span>');
}
}
});
});
});
</script>
我怎么能追加错误消息提交的表单的div.message是否正确? 任何帮助将大大AP preciated。
How can i append the error message to the submitted form's div.message correctly? Any help will greatly appreciated.
推荐答案
我觉得你可以交之前拿到味精股利。
I think you can get the msg div before post .
$(document).ready(function() {
$('.myform').on('submit', function(event) {
msgDiv=($this).children('div.message');//before post get the msg div first
event.preventDefault();
$(this).ajaxSubmit({
url: 'process.php',
type: 'post',
dataType: 'json',
success: function( response ){
$.each(response, function(){
msgDiv.append('<div>'+message+'</span>');
}
}
});
});
});
</script>
这篇关于多形式的页面 - 通过AJAX加载错误信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!