我正在尝试将一些值从输入文本框推送到 JSON 数组。这是我尝试过的
代码:
<!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>
Run Code Online (Sandbox Code Playgroud)
所以我试图通过定义一个变量 (x) 来将 'x' 推入数组中以获取输入文本框的值:
x = document.getElementById("studentnumber").value;
Run Code Online (Sandbox Code Playgroud)
尝试将其推送到第 25 行:
obj['G11S'].push({"StudentNumber": "4","status": "Member"});
Run Code Online (Sandbox Code Playgroud) 我收到一个我不理解的错误,因为我是C++的新手.该if陈述在这种情况下有问题:
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int main()
{
string username;
int password;
cout << " Welcome User, Please Login." << endl;
//Username
cout << "Username: ";
cin >> username;
//Password
cout << "\nPassword: ";
cin >> password;
if (username == 'admin' && password == '852456')
cout << "Welcome." <<endl;
else
cout << "Wrong credentials." <<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)