3

我创建了一个包含多个同名字段的表单:

<form ... >
<input type=text name="problem"  value="problem1" />
<input type=text name="question" value="question1" />
<input type=text name="answer"   value="answer1" />

<input type=text name="problem"  value="problem2" />
<input type=text name="question" value="question1" />
<input type=text name="answer"   value="answer1" />
<input type=text name="question" value="question2" />
<input type=text name="answer"   value="answer1" />
<input type=text name="answer"   value="answer2" />
...
<input type=hidden name="jsonString" value="" />

<input type=submit value="Send" />
</form>

这些字段可以通过javascript动态添加/删除

我想在隐藏字段中放入一个字符串(使用 JSON.strinfigy)并在提交之前通过表单传递:

{
    problem: [  
        {   
            name: problem 1, 
            question: [
                {
                    name: question 1,
                    answer: [
                        name:answer1
                    ]
                }
            ]
        },

        {   
            name: problem 2, 
            question: [
                {
                    name: question 1,
                    answer: [
                        name:answer1
                    ]
                },  
                {
                    name: question 2,
                    answer: [
                        name:answer1,
                        name:answer2
                    ]
                }   
            ]
        }       
    ]
}

有什么想法吗?谢谢

4

1 回答 1

0

没有任何方法可以由此序列化 json;尝试简单地创建,如下所示:

var myJson;
$("input[name=problem]").each(function(){
   myJson.Push( { "name": $this.val()});
});

(查看adding-removing-items-from-json-data-with-jquery形式更多指令)

于 2012-10-15T08:19:28.703 回答