蕪湖不蕪
2023-07-06 14:37:33
在我的腳本中,我有動態添加的輸入字段。我必須使用 php 獲取所有輸入值,但 $_POST['poids'] 中的問題只給我輸入數組的第一個值,因此只給我數組 poids 的第一個元素。這是我的代碼:$(function() { var max_fields = 10; var $wrapper = $(".container1"); var add_button = $(".add_form_field"); $(add_button).click(function(e) { e.preventDefault(); const vals = $("> .item input[name^=poids]", $wrapper).map(function() { return +this.value }).get() const val = vals.length === 0 ? 0 : vals.reduce((a, b) => a + b); if ($("> .item", $wrapper).length < max_fields && val < 100) { const $form_colis = $(".item").first().clone(); $form_colis.find("input").val(""); $wrapper.append($form_colis); //add input box } else { var err_msg = 'limit riched'; //alert(err_msg); window.alert(err_msg); } }); $wrapper.on("click", ".delete", function(e) { e.preventDefault(); $(this).parent('div').remove(); })});<div class="container1" style="min-height:200px"> <button class="add_form_field">Add New Field ?</button> <form method="post" action="postForm.php"> <div class="item"> <input type="text" placeholder="Poids" name="poids[]"> <input type="text" placeholder="Longueur" name="longueurs[]"> <input type="text" placeholder="Largeur" name="largeurs[]"> <input type="text" placeholder="Hauteur" name="hauteurs[]"> <a href="#" class="delete">Delete</a> </div> <button type="submit" name="" class="btn btn-danger btn-responsive "> Send </button></center> </a> </form></div>獲取帖子(postForm.php):$poids = $_POST['poids'];foreach($poids as $poid) { echo " -->" .$poid;}我希望你明白我的意思。先感謝您
2 回答

慕碼人8056858
TA貢獻1803條經驗 獲得超6個贊
問題是您將帶有新輸入字段的 div 附加到$wrapper
,但這在表單之外。您需要將其放入表格中。
改變
$wrapper.append($form_colis); //add input box
到
$('.item', $wrapper).last().after($form_colis); //add input box

神不在的星期二
TA貢獻1963條經驗 獲得超6個贊
我不是 PHP 專家,但通過瀏覽提供的代碼,您似乎只是在搜索名稱值為 poids 的輸入。
const vals = $("> .item input[name^=poids]",$wrapper).map(function() { return +this.value }).get()
然后,當您創建 bew 輸入時,您不會將 poids 附加到輸入名稱。
const $form_colis = $(".item").first().clone();
$form_colis.find("input").val("");
$wrapper.append($form_colis);
因此,用你的方法你只會找到一個,那就是這個:
<input type="text" placeholder="Poids" name="poids[]">
因此,為了解決這個問題,在$form_colis方法內部添加poids我相信的內容。
添加回答
舉報
0/150
提交
取消