Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 动态填充JSON数组_Javascript_Arrays_Json_Dynamic - Fatal编程技术网

Javascript 动态填充JSON数组

Javascript 动态填充JSON数组,javascript,arrays,json,dynamic,Javascript,Arrays,Json,Dynamic,每次按下按钮后,我都会尝试动态填充json数组。我的目标是稍后在txt文件中保护该数组,但这是另一个故事,为此我在W3C页面上找到了一些示例 这是我目前的代码: enter <html> <body> <table> <tr> <td><label>Subject: <input id="subject" type="text" /></label></td>

每次按下按钮后,我都会尝试动态填充json数组。我的目标是稍后在txt文件中保护该数组,但这是另一个故事,为此我在W3C页面上找到了一些示例

这是我目前的代码:

enter 
<html>
<body>

 <table>
    <tr>
        <td><label>Subject: <input id="subject" type="text" /></label></td>
        <td><label>Semester: <input id="semester" type="text" /></label></td>
        <td><label>Name: <input id="name" type="text" /></label></td>
    </tr>
    <tr>
       <td>
           <label>Question: <label>
       </td>
       <td colspan="2">
            <textarea id="question" style="width:512px;height:100px"></textarea>
       </td>
   </tr>
   <tr>
       <td>
            <label>Answer 1: <label>
        </td>
        <td colspan="2">
             <textarea id="Answer1" style="width:512px;height:100px"></textarea>
        </td>
    </tr>
    <tr>
        <td>
            <label>Answer 2: <label>
        </td>
        <td colspan="2">
            <textarea id="Answer2" style="width:512px;height:100px"></textarea>
       </td>
    </tr>
    <tr>
        <td>
            <label>Answer 3: <label>
        </td>
        <td colspan="2">
            <textarea id="Answer3" style="width:512px;height:100px"> </textarea>
       </td>
    </tr>
     <tr>
        <td>
           <label>Answer 4: <label>
        </td>
        <td colspan="2">
            <textarea id="Answer4" style="width:512px;height:100px">   </textarea>
        </td>
    </tr>
    <tr>
        <td></td>
        <td><button onclick="saveInputInArray()">Save in Array</button></td>
        <td></td>
    </tr>
</table>

<script type='text/javascript'>

var quiz = {
    question:[]
};

function saveInputInArray()
{
     quiz.question.push({
        "Subject" : document.getElementById("subject").value,
        "Semester" : document.getElementById("semester").value,
        "Name" : document.getElementById("name").value,
        "Question" : document.getElementById("question").value,
        "Answer1" : document.getElementById("Answer1").value,
        "Answer2" : document.getElementById("Answer2").value,
        "Answer3" : document.getElementById("Answer3").value,
        "Answer4" : document.getElementById("Answer4").value
    });

     alert("Semester: " + quiz["question"]["semester"]);
}

</script>

</body>
</html>
enter
主题:
学期:
姓名:
问题:
答复1:
答复2:
答复3:
答复4:
保存在数组中
变量测验={
问题:[]
};
函数saveInputArray()
{
测验、提问、推送({
“主题”:document.getElementById(“主题”).value,
“学期”:document.getElementById(“学期”).value,
“名称”:document.getElementById(“名称”).value,
“问题”:document.getElementById(“问题”).value,
“Answer1”:document.getElementById(“Answer1”).value,
“Answer2”:document.getElementById(“Answer2”).value,
“Answer3”:document.getElementById(“Answer3”).value,
“Answer4”:document.getElementById(“Answer4”).value
});
警惕(“学期:+测验[“问题”][“学期]);
}

您的代码很好,只需更改此行:

alert("Semester: " + quiz["question"][0]['Semester']);

考虑到您编写的这行代码

alert("Semester: " + quiz["question"]["semester"]);
我想要这样的:

var quiz = {
    question:[]
};

function saveInputInArray()
{

    "subject semester name question Answer1 Answer2 Answer3 Answer4".split(" ").forEach(function(id){
        quiz.question[id] = document.getElementById(id).value;
    });

    alert("Semester: " + quiz["question"]["semester"]);
}
您不会“填充json数组”。json是一种传输编码格式。填充javascsript数组,然后在填充完成后将其编码为json。