不能.ajax()提交一个已使用jQuery .load()加载到主页的php表单. [英] Cant .ajax() submit a php form that has been loaded onto main page with jQuery .load()
问题描述
我遇到以下问题.以下是有关我的PHP页面及其工作方式的说明.当我直接访问form.php并尝试通过AJAX提交它时,它可以完美运行.
I'm having the following problem. Below is an explanation of what my PHP pages are and how they work. When I access form.php directly and try to submit it via AJAX, it works perfectly.
问题-当我将form.php加载到main.php中时,form.php中的所有jQuery代码均不会触发. (通过Firebug验证)没有提交,没有警报,什么也没有.加载到main.php中后,如何在form.php中使jQuery代码正常工作?
Problem - When I .load() form.php into main.php, none of the jQuery code within form.php fires. (verified through firebug) No submits, no alerts, nothing. How can I get the jQuery code within form.php to work when its loaded into main.php?
main.php ->这是主PHP页面,上面有一个链接.单击此链接后,将触发以下jQuery代码在名为#formcontainer的div中加载"form.php".这是main.php中加载form.php的代码.
main.php -> This is the main PHP page which has a link on it. Once this link is clicked, the following jQuery code fires to load "form.php" within a div called #formcontainer. This is the code within main.php that loads form.php.
<a href="#" id="addHomeProfile">Foobar</a>
<div class="formcontainer"></div>
<script type="text/javascript">
$(document).ready(function(){
$("#addHomeProfile").click(function(){
$(".formcontaineropen").load("form.php");
});
});
</script>
form.php ->这是一个上面加载的表单.它通过jQuery .ajax()POST向MySQL提交数据.这是提交表单的jquery代码,其ID为#homeprofile.
form.php -> this is a form that gets loaded above. It submits data to MySQL through an jQuery .ajax() POST. Here is the jquery code which submits the form, which has an ID called #homeprofile.
<form id="homeprofile"> <input type="text" name="name" id="name" />
<input type="submit" value="submit" id="submit"></form>
<script type = "text/javascript">
$(document).ready(function() {
$('#homeprofile').submit(function(e){
e.preventDefault();
alert("form submitted");
$.ajax({ // Starter Ajax Call
type: "POST",
url: 'update.php',
data: $('#homeprofile').serialize(),
});
});
});
推荐答案
为此使用 on()喜欢,
$(document).on('submit','#homeprofile',function(e){
e.preventDefault();
alert("form submitted");
$.ajax({ // Starter Ajax Call
type: "POST",
url: 'update.php',
data: $('#homeprofile').serialize(),
});
return false;
});
这篇关于不能.ajax()提交一个已使用jQuery .load()加载到主页的php表单.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!