duanjue6575 2014-09-10 00:28
浏览 37

如何让表单字段动态乘法,然后通过post方法提交

I have tried several ways to get this to work, but never seem to have any success. I want the innerHTML from the below javascript to display x amount of times, based on the value selected in the dropdown box, then to submit all fields to another php which will insert into mysql. When I have tried cloning the section of HTML, it only posts the values in the last clone to the next form. I would like all values from all "cloned" portions to be sent through. Any help would be greatly appreciated!

I have the following HTML:

<form action="?page=book_add" method="post" name="add_booking" id="book" target="_self" enctype="multipart/form-data">

<center><table><tr><td class="form_label">Passengers</td><td valign="middle">
<select id="numpass" name="numpass" class="form_e"/>
<option value="1" selected="selected">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
  <option value="4">4</option>
  <option value="5">5</option>
  <option value="6">6</option>
  </select>
</td></tr></table></center>

<div id="repeat"></div>

<table width="100%"><tr><td></td><td style="text-align:right"><input type="submit" value="Add Booking" /></td></tr>
</table>
</form>

Javascript:

$(function(){
    $("#numpass").change(addInput);
});

var counter = parseInt($("#numpass").val());
var limit = 7;
function addInput(divName){
     if (counter == limit)  {
          alert("You have reached the limit of adding " + counter + " inputs");
     }
     else {
          var newdiv = document.createElement('div');
          newdiv.innerHTML = "<div class='form_head'>Passenger " + counter + " - Details</div>
<br />
<table width='100%' cellpadding='2' cellspacing='4'>

<tr>
<td class='form_label'>First Name:&nbsp;<input type='text' name='cust_fname[]' class='form_f' required='required'/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Last Name:&nbsp;<input type='text' name='cust_lname[]' class='form_f' required='required'/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Weight (kg):&nbsp;<input type='text' name='cust_kg[]' class='form_a' required='required'/></td>
</tr>

<tr>
<td class='form_label'>Phone:&nbsp;<input type='text' name='cust_phone[]' class='form_g' required='required'/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Email:&nbsp;<input type='text' name='cust_email[]' class='form_d' /></td>
</tr>

<tr>
<td>
<table width='100%' cellpadding='2' cellspacing='4'>
<tr><td class='form_label'>Address:</td></tr>
<tr>
<td class='form_label'>Number:&nbsp;<input type='text' name='cust_addr_num[]' class='form_a' />&nbsp;&nbsp;&nbsp;Street:&nbsp;<input type='text' name='cust_addr_street[]' class='form_b' />&nbsp;&nbsp;&nbsp;Suburb/Town:&nbsp;<input type='text' name='cust_addr_sub[]' class='form_f' /></td></tr>
<tr><td class='form_label'>State:&nbsp;<input type='text' name='cust_addr_state[]' class='form_b' />&nbsp;&nbsp;&nbsp;Postcode:&nbsp;<input type='text' name='cust_addr_post[]' class='form_a' />&nbsp;&nbsp;&nbsp;Country:&nbsp;<select class='form_c' name='cust_addr_country[]'>
<option value=''>--- Select Country ---</option>
<option value='Australia'>Australia</option></select></td></tr></table>
</td></tr></table>
<br />";
            $(newdiv).appendTo("#repeat");
          /*document.getElementById(divName).appendChild(newdiv);
          counter++;*/
     }
}

JS Fiddle

  • 写回答

1条回答 默认 最新

  • douzi0609 2014-09-10 00:42
    关注

    I believe you have to add the current counter value to the names of the form tags (so it will be cust_phone[1], cust_phone[2] etc).

    Form input field names containing square brackets like field[index]

    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100