[英]HELP here using ajax + jquery nested form submission
Well im new to jquery and ajax and the code below doesnt work after 2nd attemp of submit form... heres the javascript code: 好吧,我是jQuery和ajax的新手,下面的代码在第二次尝试提交表单后不起作用...这里是javascript代码:
$(document).ready(function() {
var options = {
target: '#output2', // target element(s) to be updated with server response
beforeSubmit: showRequest, // pre-submit callback
success: showResponse // post-submit callback
};
$('#myForm2').submit(function() {
$(this).ajaxSubmit(options);
return false;
});
$('#me').submit(function() {
$("#div2").load("main.php");
return false;
});
});
function showRequest(formData, jqForm, options) {
return true;
}
function showResponse(responseText, statusText, xhr, $form) {}
by the way im using jquery form plugin..... 顺便说一下,即时通讯使用jQuery的形式插件.....
and for the index.php 而对于index.php
<form id="me" action="" method="post">
Message: <input type="text" name="mess">
<input type="submit" value="submit">
lastly for the main.php 最后是main.php
<form id="myForm2" action="index.php" method="post"><div>
Name:</td><td><input name="Name" type="text" />
<input type="reset" name="resetButton " value="Reset" />
<input type="submit" name="submitButton" value="Submit1" />
</div></form>
<h1>Output Div (#output2):</h1>
<div id="output2">AJAX response will replace this content.</div>
</div>
You downloading your form after page load ( *$("#div2").load("main.php")) and your events are already binded. 您在页面加载(* $(“#div2”)。load(“ main.php”))之后下载表单,并且事件已绑定。 So when your second form loaded, it's Submit button click event doesn't triger nothing. 因此,当您加载第二个表单时,“提交”按钮的单击事件不会触发任何操作。 You have to read about .live() in jQuery here 您必须在这里阅读有关jQuery中的.live()的信息
Here is my solution (that is just simpl example): 这是我的解决方案(这只是简单的示例):
$('#myForm2').live("click",function() {
$(this).ajaxSubmit(options);
return false;
});
Maybe this will work for you. 也许这对您有用。
Also you can try this: 您也可以尝试以下方法:
1) replace 1)更换
$('#myForm2').live("click",function() {
$(this).ajaxSubmit(options);
return false;
});
with 与
function MyFormSubmit(form) {
$(form).ajaxSubmit(options);
return false;
})
2) Add JS code to you myForm2 Submit button onClick
event 2)向您添加JS代码myForm2提交按钮onClick
事件
<form id="myForm2" action="index.php" method="post"><div>
Name:</td><td><input name="Name" type="text" />
<input type="reset" name="resetButton " value="Reset" />
<input type="submit" name="submitButton" value="Submit1"
onClick="javascript:MyFormSubmit(this);return false;"/>
</div></form>
<h1>Output Div (#output2):</h1>
<div id="output2">AJAX response will replace this content.</div>
</div>
You have to remove action="index.php" method="post"
from your form. 您必须从表单中删除action="index.php" method="post"
。 Then should it work. 那应该工作。
because now it is still making the php post to index.php 因为现在它仍然在将PHP发布到index.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.