为什么不能往json里push数据?
下面代码不管用,调式时控制台显示SyntaxError: unterminated regular expression literal,也看不懂,找了一上午也没找到原因
<!DOCTYPE html>
<html>
<head>
<title>json push还不管用</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js" type="text/javascript"></script>
<style>
ul {margin: 0; padding: 0;}
li { list-style: none;}
.txt { padding-left: 4px; outline: none;}
#ul{
color:red;
border:solid 1px blue;
}
</style>
</head>
<body>
<form action="#" method="get">
<label>
<input id="txt1" class="txt" type="text" name="UserName" placeholder="请输入姓名">
<input id="txt2" class="txt" type="text" name="Userscore" placeholder="请输入分数">
</label>
<input id="bsave" type="button" value="保存数据">
<input id="bshow" type="button" value="显示数据">
</form>
<ul id="myul">
<li id='xs'>下面是学生分数信息</li>
</ul>
<script>
var $name = $('#txt1');
var $score =$('#txt2') ;
var $txt = $('.txt');
//定义一个json对象,用于保存学生的相关资料
var myjson = "[{'name':'a','score':1},{'name':'b','score':2}]";
//push内容
var myarr = {
"name" : $name.val(),
"score" : $score.val()
};
$('#bsave').on('click', function () {
myjson.push(myarr);
$txt.val('');
});
$('#bshow').on('click',function () {
//通过$.each()工具函数,获取数组中各元素的名称与内容,显示在页面中。
$.each(myjson,function (index,obj) {
$('ul').append('<li>'+'姓名:'+obj.name+' 年龄:'+obj.socre</li>');
});
</script>
</body>
</html>麻烦大家帮忙看一下,谢了