最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Pushing input values Into a JSON Array - Stack Overflow

programmeradmin2浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 5

You 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);
}
发布评论

评论列表(0)

  1. 暂无评论