gpt4 book ai didi

javascript - Php 中未捕获动态添加的输入值

转载 作者:行者123 更新时间:2023-11-28 17:51:06 26 4
gpt4 key购买 nike

我有一个表单,我使用 jquery 动态添加输入字段,然后使用 Laravel Php 提交该表单的值

我的html:

<div class="input_fields_wrap">
<button class="add_field_button">Add More Fields</button>
<div>
<input type="text" name="mytext[]">
</div>
</div>

我的脚本:

<script type="">
$(document).ready(function() {
var max_fields = 10; //maximum input boxes allowed
var wrapper = $(".input_fields_wrap"); //Fields wrapper
var add_button = $(".add_field_button"); //Add button ID

var x = 1; //initlal text box count
$(add_button).click(function(e){ //on add input button click
e.preventDefault();
if(x < max_fields){ //max input box allowed
x++; //text box increment
$(wrapper).append('<div><input type="text" name="mytext[]"/><a href="#" class="remove_field">Remove</a></div>'); //add input box
}
});

$(wrapper).on("click",".remove_field", function(e){ //user click on remove text
e.preventDefault(); $(this).parent('div').remove(); x--;
})
});
</script>

问题是,当我提交在输入框中输入的值并尝试使用以下 Laravel 代码捕获它们时。

$mytext =Request::get('mytext');
dd($mytext);

无论我添加多少个文本输入,都只会捕获我在 html 中定义的输入的第一个值。

输出:

array:1 [▼
0 => "Text box 1"
]

如何解决这个问题?谢谢

最佳答案

您可以为添加的每个输入添加不同的名称。

您的 HTML:

<input type="text" name="mytext[text]">

你的 JS:

var x = 1; //initlal text box count
$(add_button).click(function(e){ //on add input button click
e.preventDefault();
if(x < max_fields){ //max input box allowed
$(wrapper).append('<div><input type="text" name="mytext[text'+ x++ +']"/><a href="#" class="remove_field">Remove</a></div>'); //add input box
}
});

输出将是:

"mytext" => array:4 [▼
"text" => "Text"
"text1" => "Text1"
"text2" => "Text2"
"text3" => "Text3"
]

希望这有帮助!

关于javascript - Php 中未捕获动态添加的输入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45495444/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com