将数据写入JSON

时间:2013-02-28 05:01:07

标签: javascript html json html5

可以读取JSON的值。同样,我们可以在单击“提交”按钮时将文本框值写入JSON变量

例:`

<html>
<head>
<title>
Sample Employee Details
</title>
<script type="text/javascript" src="emp_json.js">
</script>
</head>
<body>
<form>
<p>Emp ID:<input type="text" id="empid" /></p>
<p>Name:<input type="text" id="name" /></p>
<p>Mobile:<input type="text" id="mob" /></p>
<p><input type="button" value="Add" onclick="add();"/></p>
</form>
<script>
function add()
{
    EmployeeDetails.EmployeeID = document.getElementById('empid').value;
    EmployeeDetails.Name = document.getElementById('name').value;
    EmployeeDetails.Mobile = document.getElementById('mob').value;alert("hi");
}
</script>
</body>
</html>`

我的JSON:

var EmployeeDetails = new Array();

EmployeeDetails = [
                    {
                    "EmployeeID":"",
                    "Name":"",
                    "Mobile":""
                    }
                ];

1 个答案:

答案 0 :(得分:1)

也许您正在尝试使用jQuery库中的parseJSON