简体   繁体   English

jQuery-在父页面的DIV中加载表单提交(?)

[英]jQuery - Loading form submission in DIV of parent page (?)

I have the following code, which works perfectly ... so far. 我有以下代码,到目前为止效果很好。 But I want to next have a form load ... read below this code for an explanation. 但是我接下来要加载表单...请阅读下面的代码以获取解释。

Main Page: 主页:

<li><a href="content1.php" class="load-external" data-target="#right_section">Some Content</a></li>
<div id="right_section"></div>

<script>
$('body').on('click', 'a.load-external', function(e){
var target = $( $(this).attr('data-target') );
target.load( $(this).attr('href') );
e.preventDefault();
});
</script>

content1.php content1.php

<li><a href="content2.php" class="load-external" data-target="#right_section">Some Content 2</a></li>

Now, in content1.php , all links (a=href... ) work great and load into the #right_section div. 现在,在content1.php中,所有链接(a = href ...)都可以正常工作,并加载到#right_section div中。

But on content1.php, I also have some forms, and I would like them to also load into the same div #right_section. 但是在content1.php上,我也有一些表格,我希望它们也加载到同一div #right_section中。 A sample of a form is as follows: 表单示例如下:

sample form in content1.php: content1.php中的样本表单:

<form action="report_output.php" method="get" id="graphIt">

How can I get a form (any form, might be multiple on the page) to load into the div #right_section, without loading over the entire page (which it does now) 我如何才能获取一个表格(任何形式,可能在页面上多个)加载到div #right_section中,而又不加载整个页面(现在这样做)

Thanks ! 谢谢 !

You'll need to replace the form's submit handler, similarly to the way you replaced the normal click handler on links. 您需要替换表单的提交处理程序,类似于替换链接上的常规单击处理程序的方式。

HTML: HTML:

<form action="report_output.php" class="load-external" method="get" id="graphIt" data-target="#right_section">

JS: JS:

$('body').on('submit', 'form.load-external', function(e) {
    e.preventDefault();
    var target = $($(this).data('target'));
    $.ajax({
        url: $(this).attr('action'),
        type: $(this).attr('method'),
        data: $(this).serialize(),
        dataType: 'html',
        success: function(response) {
            target.html(response);
        }
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM