2017-08-29 51 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