2017-08-29 89 views
0

我建立一个Django的网站,我的代码是:如何一次提交两篇文章?

<script type="text/javascript"> 
 

 
$("#lxwjsubmit").click(function(){ 
 
    var userId=$('#id_user1').val(); 
 
    var password=$('#id_password1').val(); 
 
    var newtable2='<table id="table2"><tr><th></th><td><input type="text" name="userId" value="'+userId+'" /></td></tr><tr><th></th><td><input type="password" name="password" maxlength="100" value="'+password+'" /></td></tr></table>' 
 
     
 
    $('#table2').html(newtable2);  
 
    document.getElementById('form2').submit();  
 
    document.getElementById('form1').submit(); 
 
}) 
 
</script>
<div class="form-group"> 
 
      <form action="" method="POST" id='form1'> 
 
       <table> 
 
        <tr><th></th><td><input type="text" name="user" id="id_user1" /></td></tr> 
 
        <tr><th></th><td><input type="password" name="password" id="id_password1" /></td></tr> 
 
       </table> 
 

 
       <input type="button" value="submit" id='lxwjsubmit'> 
 
      </form> 
 
</div> 
 

 

 
<div class="form-group"> 
 
      <form action="http://localhost/Login.do" method="POST" id='form2' hidden='true' >   
 
     
 
       <table id="table2"> 
 
       <tr><th></th><td><input type="text" name="userId" /></td></tr> 
 
       <tr><th></th><td><input type="password" name="password" /></td></tr> 
 
       </table> 
 
       <input type="button" value="submit" > 
 
      </form> 
 
</div>

我可以单独提交每个窗体。当放在一起时,只有一种形式可以被引用。 你能纠正我吗?还是有更优雅的方式来提交两种不同的形式?

+0

您需要使用AJAX。 Google for AJAX表单提交 - 很多指南存在 – Milney

+1

为什么你甚至想要这样做?这真的很奇怪。 – Martijn

回答

0

不能一气呵成提交多份表格,你只能提交每提交一个表单。有一些选项:

  • 一个大单将其更换,并使用<fieldset>,你现在有你<form>
  • 在提交第一个表单时,获取其他表单的值并将这些值通过隐藏字段添加到第一个表单中。需要一点JavaScript,并且需要维护。
  • 您可以使用AJAX post()提交它们(合并或分离),但这必须适合您的情况。 Ajax可能使事情比需要更复杂一点。

还有更多的解决方法。我推荐第一个,因为它可以让您的代码更清晰,更易于理解。没有多少人希望一次提交两种表格。

+0

我要发帖给两个不同的动作,如何使用

? – skystone

+0

他们ajax是唯一的出路。正如我所说,一次在线的一种形式(因此一次活动)。 – Martijn

0
$("#form2").ajaxSubmit({url: 'server.php', type: 'post'}) 
$("#id_user1").ajaxSubmit({url: 'server.php', type: 'post'}) 

ajax submit jQuery