[英]Jquery serialize form not iniating,
I am loading a form via jquery load, which populates the content div, the form etc is showing up fine, but when I go to update the form with values it doesn't trigger my on("submit"
我正在通过jquery加载加载一个表单,该表单填充了内容div,表单等显示得很好,但是当我用值更新表单时,它不会触发我的
on("submit"
My jquery 我的jQuery
<!-- process update profile form without refreshing, then load the div with the new information !-->
$("#content").on("submit", "#update_profile_form", function() {
alert("jfjf");
$.ajax({
type: $(this).attr('method'),
url: $(this).attr('action'),
headers: {'X-Requested-With': 'XMLHttpRequest'},
data: $(this).serialize(),
success: function(data) {
if(data.success == "false"){
$.notify(data.error, "error");
}else{
$.notify(data.success, "success");
$("#content").load("<?php echo Config::get('URL'); ?>user" + " #inner_main_content");
}
}
});
return false; <!-- important: prevent the form from submitting !-->
});
<!-- end process update profile form without refreshing, then load the div with the new information !-->
My form: 我的表格:
<form action="<?php echo Config::get('URL'); ?>/user/update_personal_information" method="POST" id="update_profile_form">
<!-- inputs here !-->
<input type="submit" class="btn vd_btn vd_bg-green col-md-offset-10" name="submit" id="submit" value="<?php echo System::translate("Update"); ?>">
<span class="menu-icon">
<i class="fa fa-fw fa-check"></i>
</span>
I am simply loading the form in to content via this code: 我只是通过以下代码将表单加载到内容中:
//populate the div
$('.list-group-item').click(function(e){
e.preventDefault();
var link = $(this).attr('href');
$('#content').fadeOut('slow', function(){
$("#content").load( link + " #inner_main_content", function(){
$('#content').fadeIn('slow');
});
});
The alert trigger in the on submit isn't even popping up which makes me beleive that it's not triggering, and I am receiving no errors neither. 提交中的警报触发甚至没有弹出,这使我相信它没有触发,而且我也没有收到任何错误。 I am binding the on submit to the content id because that's the main div it loads to, so how can I iniate the form to work?
我将on Submit绑定到内容ID,因为这是加载的主要div,那么如何初始化表单呢?
Here's the full form: http://pastebin.com/iGsPZmWT 这是完整的表格: http : //pastebin.com/iGsPZmWT
I'm not sure to clearly understand the whole of your architecture, nor the precise case of your issue, but here is how I can summarize what I understand: 我不确定要清楚地了解您的整个体系结构,也不是要确切地了解问题的确切情况,但是这里是我可以总结自己所了解的内容的方法:
#content
part includes the #submit
element #content
部分包含#submit
元素
#content
part is first the direct content of the initial page, and is functional (triggers on submit) #content
部分首先是初始页面的直接内容,并且具有功能(在提交时触发)
#content
part is overwritten by the .load()
process .load()
进程将覆盖#content
部分 #content
part becomes non-functional (nothing happens on submit) #content
部分将无法正常工作(提交时未发生任何事情) If it is really so, then the issue is pretty "normal": the submit
event you attached to #content
is lost when you load a new #content
content (sorry for the repeat, but it comes from the id you'd choosen!). 如果真的是这样,那么这个问题是很“正常”: 在
submit
您连接到事件#content
,当你加载一个新的丢失#content
内容 (遗憾的重复,但它来自你choosen的ID! )。
Even if the new content also includes a #submit
element, this one has no event attached to it now. 即使新内容还包含一个
#submit
元素,该内容现在也没有附加事件。
One solution is to execute the attachment again when a new content has been loaded, like this: 一种解决方案是在加载新内容后再次执行附件,如下所示:
function attachSubmit() {
$("#content").on("submit", "#update_profile_form", function() {
alert("jfjf");
$.ajax({
type: $(this).attr('method'),
url: $(this).attr('action'),
headers: {'X-Requested-With': 'XMLHttpRequest'},
data: $(this).serialize(),
success: function(data) {
if(data.success == "false"){
$.notify(data.error, "error");
}else{
$.notify(data.success, "success");
$("#content").load("<?php echo Config::get('URL'); ?>user" + " #inner_main_content");
attachSubmit(); // <<-- here we attach event again to new content
}
}
});
return false; <!-- important: prevent the form from submitting !-->
});
}
(note that you also have to initially invoke attachSubmit()
somewhere in a $(document).ready()
sequence) (请注意,您还必须首先在
$(document).ready()
序列中的某个地方调用attachSubmit()
)
Alternatively you might attach a delegated event, so once is enough. 或者,您可以附加一个委托事件,所以一次就足够了。 In this case, though, you'll have to attach it to a parent element of
#content
, instead of #content
itself, something like this: 不过,在这种情况下,您必须将其附加到
#content
的父元素,而不是#content
本身,如下所示:
// Here #parent is supposed to be a parent of #content
// The on() method got a #submit arg, as selector for delegation
$("#parent").on("submit", "#submit", "#update_profile_form", function() {
alert("jfjf");
$.ajax({
// ... same as YOUR previous version (nothing added)
});
return false; <!-- important: prevent the form from submitting !-->
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.