jQuery - 如何让表单的动作显示在表单所在的同一个div中?

时间:2017-11-28 17:45:40

标签: javascript php jquery html

我有三页new.htmlsomepage.htmlsomeotherpage.html

new.html

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js" type="text/javascript"></script>
<script>
 $(document).ready(function() {
  $('#nav a').click(function(e) {
   e.preventDefault();
   $('#content').load($(this).attr('href'));
  });
 });
</script>

<div id="nav"><a href="somepage.html">Some page</a></div>

<div id="content">show the stuff</div>

点击somepage.html链接时,它会显示在div,其ID为content

somepage.html

<form id="form" action="someotherpage.html" method="post">
<input type="submit" value="test"/>
</form>

表单的操作转到新页面,而我希望它只在实际表单所在的div内。

1 个答案:

答案 0 :(得分:0)

您必须做与第一页非常相似的事情,在表单的兄弟中加载someotherpage.html

somepage.html :

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js" type="text/javascript"></script>

<script>
$('#form').submit(function(e) {
    e.preventDefault();
    $('#target').load($(this).attr('action'));
});
</script>

<div id="formWrapper">
    <form id="form" action="someotherpage.html" method="post">
        <input type="submit" value="test"/>
    </form>
    <div id="target"></div>
</div>