使用jquery动态添加表单字段而不是发布 [英] Dynamically add form fields with jquery not posting
问题描述
我成功地设置了表单并使其工作。但是我有一个问题
I have succeeded in setting up the form and making it work. However I have a issue
我从这个获得了这个Jquery
I got this Jquery from this
一切正常但是当我发布我的数据到tcpdf新创建的行没有发送,并且我想要添加到数据库的平均时间,请任何人都知道答案非常欣赏。
这里是我的代码
Everything works fine but when I post my data to a tcpdf newly created row didnt send, and mean time I want to add to the database also, please anyone knows the answer much appreciate. here is my codes
<script type="text/Javascript">
$(document).ready(function() {
$('#btnAdd').click(function() {
var num = $('.clonedSection').length;
var newNum = new Number(num + 1);
var newSection = $('#pq_entry_' + num).clone().attr('id', 'pq_entry_' + newNum);
newSection.children(':first').children(':first').attr('id', 'quantity_' + newNum).attr('name', 'quantity_' + newNum);
newSection.children(':nth-child(2)').children(':first').attr('id', 'rcptdelvry_' + newNum).attr('name', 'rcptdelvry_' + newNum);
newSection.children(':nth-child(2)').children(':first').attr('id', 'weight_' + newNum).attr('name', 'weight_' + newNum);
newSection.children(':nth-child(2)').children(':first').attr('id', 'volume_' + newNum).attr('name', 'volume_' + newNum);
newSection.insertAfter('#pq_entry_' + num).last();
$('#btnDel').prop('disabled','');
if (newNum == 5)
$('#btnAdd').prop('disabled','disabled');
});
$('#btnDel').click(function() {
var num = $('.clonedSection').length; // how many "duplicatable" input fields we currently have
$('#pq_entry_' + num).remove(); // remove the last element
// enable the "add" button
$('#btnAdd').prop('disabled','');
// if only one element remains, disable the "remove" button
if (num-1 == 1)
$('#btnDel').prop('disabled','disabled');
});
$('#btnDel').prop('disabled','disabled');
});
</script>
这是我的Html部分
Here is my Html part
<fieldset>
<ul id="pq_entry_1" class="clonedSection">
<label class="textfield">QUANTITY
<li><input type="text" name="quantity_1" id="quantity_1"></li>
<br class="clear">
</label>
<label class="textfield">TYPE<br>
<li><select name="rcptdelvry_1" id="rcptdelvry_1">
<option value="">-- Please Select --</option>
<optgroup label="LCL CARGO">
<option value="PACKAGES">PACKAGES</option>
<option value="CARTONS">CARTONS</option>
<option value="BALES">BALES</option>
<option value="BOX">BOX</option>
<option value="PALETTES">PALETTES</option>
<option value="CRATE">CRATE</option>
</optgroup>
<optgroup label="FULL CONTAINER">
<option value="20' Bulk (with roof hatches)">20' Bulk (with roof hatches)</option>
<option value="20' Collapsible Flat Rack">20' Collapsible Flat Rack</option>
<option value="20' Dry">20' Dry</option>
<option value="20' Flat Rack">20' Flat Rack</option>
<option value="20' Highvent w/o roof hatches">20' Highvent w/o roof hatches</option>
<option value="20' Open Top">20' Open Top</option>
<option value="20' Porthole (conair)">20' Porthole (conair)</option>
<option value="20' Reefer">20' Reefer</option>
<option value="20' Tank">20' Tank</option>
<option value="40' Artificial Tweendeck">40' Artificial Tweendeck</option>
<option value="40' Collapsible Flat Rack">40' Collapsible Flat Rack</option>
<option value="40' Dry">40' Dry</option>
<option value="40' Flat Rack">40' Flat Rack</option>
<option value="40' High Cube">40' High Cube</option>
<option value="40' High Cube Reefer">40' High Cube Reefer</option>
<option value="40' Open Top">40' Open Top</option>
<option value="40' Starvent (9'6)">40' Starvent (9'6)</option>
<option value="40' Tank">40' Tank</option>
<option value="45' High Cube">45' High Cube</option>
<option value="45' High Cube Reefer">45' High Cube Reefer</option>
</optgroup>
</select></li>
<br class="clear">
</label>
<label class="textfield">WEIGHT (kg per container)
<li><input type="tel" name="weight_1" id="weight_1"></li>
<br class="clear">
</label>
<label class="textfield">VOLUME (cbm):
<li><input type="tel" name="volume_1" id="volume_1"></li>
<br class="clear">
</label>
</ul>
</fieldset>
<input type='button' id='btnAdd' size="15" value='add another row' />
<input type='button' id='btnDel' value='delete row' />
</div>
</div>
这是我的tcpdf部分
Here is my tcpdf part
<td width="0" height="0">
: {$_POST['quantity_1']}X{$_POST['rcptdelvry_1']}{$_POST['quantity_2']}X{$_POST['rcptdelvry_2']}
</td>
推荐答案
使用标准的提交按钮提交表单将在这种情况下不起作用。
提交按钮不会占用页面加载后创建的帐户中的表单域。
Submitting your form with a standard 'submit' button will not work in this case. The submit button does not take the formfields in account that are created after page load.
您必须编写一个序列化数据的提交处理程序,将它发送到服务器。
You'll have to write a submit handler that serializes your data and sends it to the server.
尝试这样的事情:
$('#yourFormID').on('submit', function(e) {
//prevent the default submithandling
e.preventDefault();
//send the data of 'this' (the matched form) to yourURL
$.post('yourURL', $(this).serialize());
});
服务器端你现在有$ _POST变量填充了键和值。
serverside you now have the $_POST variable filled with the keys and values.
您还可以检查在firebug或任何其他webdeveloper工具中发送的数据。
You can also inspect the data that is send in firebug or any other webdeveloper tool.
编辑:
我改变了你的小提琴来处理我的代码:
I have altered your fiddle to work with my code:
我已经包装了< ;表格>< / form>
围绕您的输入并添加< input type ='submit'/>
按钮
I have wrapped a <form></form>
around your inputs and added a <input type='submit' />
button
这篇关于使用jquery动态添加表单字段而不是发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!