在自定义数组 JavaScript 中创建具有未知数量输入的 JSON


Create a JSON with unknown number of inputs in custom array JavaScript

while($ques = mysql_fetch_array($query)){
    <input type='text' name='answers' test_id='".$ques['test_id']."' question_no ='[".$ques['question_no']."]'>
}

输出为:

<input type='text' name='answers' test_id="1" question="1" >
<input type='text' name='answers' test_id="1" question="2" >
<input type='text' name='answers' test_id="1" question="3" >
<input type='text' name='answers' test_id="2" question="1" >
<input type='text' name='answers' test_id="2" question="2" >
<input type='text' name='answers' test_id="2" question="3" >

JAVA 脚本是:

var frm = $('#test-set-form');
var data = JSON.stringify(frm.serializeArray());

预期输出为:

[
    {"name":"answerswers","test_id":"1","question_no":"1",value":"<input value>"},
    {"name":"answerswers","test_id":"1","question_no":"2",value":"<input value>"},
    {"name":"answerswers","test_id":"1","question_no":"3",value":"<input value>"},
    {"name":"answerswers","test_id":"2","question_no":"1",value":"<input value>"},
    {"name":"answerswers","test_id":"2","question_no":"2",value":"<input value>"},
    {"name":"answerswers","test_id":"2","question_no":"3",value":"<input value>"},
]

我正在从数量不变的输入创建一个 JSON,并发送到服务器并将每个数据保存到数据库。一些建议?提前谢谢。

我像这样使用 smth:

while($ques = mysql_fetch_array($query)) {
    <input type='text' value=''
        name='"answerswers[{$ques['test_id']}][{$ques['question_no']}]"'>
}

外:

[
    { "key":"answerswers[1][1]", value":"" },
    { "key":"answerswers[1][2]", value":"" },
    //...
    { "key":"answerswers[5][15]", value":"" }
    //...
]

如果你只需要使用你的符号,那么用JS解析html:

var $inputs = $('#form').find('input[name=answers]');
var data = [];
$inputs.each(function() {
    var attrsData = {};
    attrsData['name'] = $(this).attr('name');
    ...
    data.push(attrsData)
});

你会得到你想要的

$.ajax({
    data: data
    ...
})

最简单的方法是更改表单元素name属性以使用数组,这样您将拥有:

<input name="tests[the_test_id][questions][the_question_id]" />

所以在 PHP 方面是 $_POST 这看起来像:

Array(
    'tests' => Array(
        'the_test_id' => Array(
            'questions' => Array(
               'the_question_id' => 'the answer value'
            )
        )
    )
)

然后你可以用这样的东西来阅读它:

foreach($_POST['tests'] as $test_id => $testData) {
    foreach ($testData['questions'] as $question_no => $answer) {
        // update the db using $test_id, $question_no, and $answer
    }
}

因此,元素的输出如下所示:

<?php 
     printf('<input type="text" name="tests[%s][questions][%s] />',
         $ques['test_id'],
         $ques['question_no']); ?>

通过这样做,如果你正在做ajax,你可以简单地在from JS上使用$(selectorForTheForm).serialize(),或者如果你不需要ajax,只使用默认的表单提交过程。

当然,如果您出于某种原因确实需要 JSON 格式的它,那么这不是一个好主意,但如果您只需要 POST 表单(ajax 与否),那么这将正常工作。

如果您确实需要 JSON 格式的它,那么除此之外,您还可以添加添加数据元素(仅仅是因为它比尝试从 idname 属性中解析数据更容易且更不容易出错),例如:

<input name="tests[the_test_id][questions][the_question_id]" data-test_id="the_test_id" data-question_no="the_question_id" />

这将使您的输出看起来像这样:

<?php 
     printf('<input type="text" name="tests[%s][questions][%s] data-test_id="%s" data-question_no="%s"/>',
         $ques['test_id'],
         $ques['question_no'],
         $ques['test_id'],
         $ques['question_no']); ?>

所以现在在JS端你可以做:

var data = [],
    $inputs = $(selectorForTheInputs);
$inputs.each(function (){
    var $this = $(this),
        datum = $this.data();
    data.push($.extend({value: $this.val()}, datum));
});

工作小提琴:http://jsfiddle.net/sbmejzdo/