0

我正在动态构建一个页面。此页面需要从输入标签中读取信息,但它是动态的。我应该将东西设置为数组,因为我正在查看

我想保留数据集。

<script>
   function adjustPilots(){
var pilots = $("#numPilots").val();
var info = '<td><table>'+
    '<tr><td>Minumum Collateral</td><td colspan="2"><input type = "text" size = "10" maxLength = "6" /> to <input type = "text" size = "10" maxLength = "6" /></td></tr>'+
    '<tr><td>Reward</td><td colspan="2"><input type = "text" size = "10" maxLength = "6" /> to <input type = "text" size = "10" maxLength = "6" /></td></tr>'+
    '<tr><td>Volume</td><td colspan="2"><input type = "text" size = "10" maxLength = "7" /> to <input type = "text" size = "10" maxLength = "7" /></td></tr>'+
    '<tr><td>Start: </td><td><input type = "text" name = "s" id = "s" class = "s" value autocomplete = "off"></td></tr>'+
    '<tr><td>End: </td><td><input type = "text" name = "e" id = "e" class = "e" value autocomplete = "off"></td></tr>'+
    '</table></td>';


for(var i = 0; i < Number(pilots); i++){
    $("#pilotrow").append(info);
}
}
</script>
<body>
<form>
<table>
<tr><td>Number of Pilots</td><td colspan="2"><input id = "numPilots" type = "text" size="3" maxLength="3" onchange = 'adjustPilots()' /></td></tr>
<tr id = "pilotrow"></tr>

<tr><td><input type = "submit" name = "submit"></td></tr>
</table>
</form>
</body>

我正在考虑的一个选项是不使用表单,而是使用 javascript 构建它。然后制作一个 JSON 对象并使用 AJAX 将其发送到服务器。这是一种可靠的方法,还是有更好的主意?

4

3 回答 3

3

There are at least 2 way to do that.

Without javascript, you cate a form with array of element like this

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

in php

$inputs = $_POST['input'];
for($inputs as $inp){

}

With ajax and jquery, you can just simply serialize your form and post to backend

于 2012-08-22T01:30:14.920 回答
2

您可以通过使用name输入中的属性来实现。像这样:

<input type="text" name="pilots[]" />

然后,您可能希望跟踪您添加的飞行员数量,这样您就可以发送索引数组。像这样:

<input type="text" name="pilots[0][minumumCollatural]" />
<input type="text" name="pilots[0][reward]" />
<input type="text" name="pilots[0][volume]" />

这样,当您将表单提交到服务器时,您的飞行员数组将如下所示:

$pilots = $_POST['pilots'];

// Which looks like
array(
   [0] => array
          (
             [minumumCollatural] => // Some number
             [reward] => // Some number
          )
    [1] => array
          (
             [minumumCollatural] => // Some number
             [reward] => // Some number
          )
)
于 2012-08-22T01:41:11.533 回答
0

尝试利用隐藏的输入标签以您喜欢的任何方式发送数据。例如:

<input type="hidden" name="myinput" id="myinput" />

现在在 JS 中:

$("form").submit(function() {
    $("input").not("#myinput").each(function() {
        $("#myinput").val($("#myinput").val()+$(this).val());
        // you can format anyway you want this is just an example
    });
});

希望这可以帮助!

于 2012-08-22T01:38:24.027 回答