admin管理员组文章数量:1025290
I am trying to push some values from the input textbox to the JSON array. Here is what i tried
Code:
<!DOCTYPE html>
<html>
<body>
<input id="studentnumber" placeholder="Student Number"></input>
<input id="status" placeholder="Status"></input>
<button id="bt1" onclick="newUser()">Validate</button>
<p id="demo"></p>
<script type="text/javascript">
//Array
var jsonStr =
'{"G11S":[{"StudentNumber":"1","status":"Pass"},{"StudentNumber":"2","status":"Pass"},{"StudentNumber":"3","status":"Pass"}]}';
function newUser(){
x = document.getElementById("studentnumber").value;
//Debug
console.log(x);
var obj = JSON.parse(jsonStr);
obj['G11S'].push({"StudentNumber": "4","status": "Member"}); // <~~ What am I Supposed to replace the "4" with and the "Member aswell"
jsonStr = JSON.stringify(obj);
console.log(jsonStr);
}
</script>
</body>
</html>
So I am trying to push 'x' into the array by defining a variable (x) to get the input textbox's value:
x = document.getElementById("studentnumber").value;
Trying to push it on line 25:
obj['G11S'].push({"StudentNumber": "4","status": "Member"});
I am trying to push some values from the input textbox to the JSON array. Here is what i tried
Code:
<!DOCTYPE html>
<html>
<body>
<input id="studentnumber" placeholder="Student Number"></input>
<input id="status" placeholder="Status"></input>
<button id="bt1" onclick="newUser()">Validate</button>
<p id="demo"></p>
<script type="text/javascript">
//Array
var jsonStr =
'{"G11S":[{"StudentNumber":"1","status":"Pass"},{"StudentNumber":"2","status":"Pass"},{"StudentNumber":"3","status":"Pass"}]}';
function newUser(){
x = document.getElementById("studentnumber").value;
//Debug
console.log(x);
var obj = JSON.parse(jsonStr);
obj['G11S'].push({"StudentNumber": "4","status": "Member"}); // <~~ What am I Supposed to replace the "4" with and the "Member aswell"
jsonStr = JSON.stringify(obj);
console.log(jsonStr);
}
</script>
</body>
</html>
So I am trying to push 'x' into the array by defining a variable (x) to get the input textbox's value:
x = document.getElementById("studentnumber").value;
Trying to push it on line 25:
obj['G11S'].push({"StudentNumber": "4","status": "Member"});
Share
Improve this question
edited Mar 1, 2016 at 17:44
Sarantis Tofas
5,1671 gold badge25 silver badges36 bronze badges
asked Mar 1, 2016 at 16:48
VancerVancer
851 gold badge3 silver badges11 bronze badges
3
- Its due to the defined variable ' x ', being placed in the statement that pushes data in at line 25 – Vancer Commented Mar 1, 2016 at 17:03
- try my answer, is this what you want? If not can you make it a little bit clear of what you need? – Sarantis Tofas Commented Mar 1, 2016 at 17:05
- @Akis_Tfs **Thank you for your help brother ** works like a charm! – Vancer Commented Mar 1, 2016 at 17:09
1 Answer
Reset to default 5You will have to get the input values and store them in an object variable then push that variable in the JSON array:
function newUser(){
var user = {};
user.StudentNumber = document.getElementById("studentnumber").value;
user.status = document.getElementById("status").value;
//Debug
console.log(user);
var obj = JSON.parse(jsonStr);
obj['G11S'].push(user);
jsonStr = JSON.stringify(obj);
console.log(jsonStr);
}
I am trying to push some values from the input textbox to the JSON array. Here is what i tried
Code:
<!DOCTYPE html>
<html>
<body>
<input id="studentnumber" placeholder="Student Number"></input>
<input id="status" placeholder="Status"></input>
<button id="bt1" onclick="newUser()">Validate</button>
<p id="demo"></p>
<script type="text/javascript">
//Array
var jsonStr =
'{"G11S":[{"StudentNumber":"1","status":"Pass"},{"StudentNumber":"2","status":"Pass"},{"StudentNumber":"3","status":"Pass"}]}';
function newUser(){
x = document.getElementById("studentnumber").value;
//Debug
console.log(x);
var obj = JSON.parse(jsonStr);
obj['G11S'].push({"StudentNumber": "4","status": "Member"}); // <~~ What am I Supposed to replace the "4" with and the "Member aswell"
jsonStr = JSON.stringify(obj);
console.log(jsonStr);
}
</script>
</body>
</html>
So I am trying to push 'x' into the array by defining a variable (x) to get the input textbox's value:
x = document.getElementById("studentnumber").value;
Trying to push it on line 25:
obj['G11S'].push({"StudentNumber": "4","status": "Member"});
I am trying to push some values from the input textbox to the JSON array. Here is what i tried
Code:
<!DOCTYPE html>
<html>
<body>
<input id="studentnumber" placeholder="Student Number"></input>
<input id="status" placeholder="Status"></input>
<button id="bt1" onclick="newUser()">Validate</button>
<p id="demo"></p>
<script type="text/javascript">
//Array
var jsonStr =
'{"G11S":[{"StudentNumber":"1","status":"Pass"},{"StudentNumber":"2","status":"Pass"},{"StudentNumber":"3","status":"Pass"}]}';
function newUser(){
x = document.getElementById("studentnumber").value;
//Debug
console.log(x);
var obj = JSON.parse(jsonStr);
obj['G11S'].push({"StudentNumber": "4","status": "Member"}); // <~~ What am I Supposed to replace the "4" with and the "Member aswell"
jsonStr = JSON.stringify(obj);
console.log(jsonStr);
}
</script>
</body>
</html>
So I am trying to push 'x' into the array by defining a variable (x) to get the input textbox's value:
x = document.getElementById("studentnumber").value;
Trying to push it on line 25:
obj['G11S'].push({"StudentNumber": "4","status": "Member"});
Share
Improve this question
edited Mar 1, 2016 at 17:44
Sarantis Tofas
5,1671 gold badge25 silver badges36 bronze badges
asked Mar 1, 2016 at 16:48
VancerVancer
851 gold badge3 silver badges11 bronze badges
3
- Its due to the defined variable ' x ', being placed in the statement that pushes data in at line 25 – Vancer Commented Mar 1, 2016 at 17:03
- try my answer, is this what you want? If not can you make it a little bit clear of what you need? – Sarantis Tofas Commented Mar 1, 2016 at 17:05
- @Akis_Tfs **Thank you for your help brother ** works like a charm! – Vancer Commented Mar 1, 2016 at 17:09
1 Answer
Reset to default 5You will have to get the input values and store them in an object variable then push that variable in the JSON array:
function newUser(){
var user = {};
user.StudentNumber = document.getElementById("studentnumber").value;
user.status = document.getElementById("status").value;
//Debug
console.log(user);
var obj = JSON.parse(jsonStr);
obj['G11S'].push(user);
jsonStr = JSON.stringify(obj);
console.log(jsonStr);
}
本文标签: javascriptPushing input values Into a JSON ArrayStack Overflow
版权声明:本文标题:javascript - Pushing input values Into a JSON Array - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745618344a2159424.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论