如何将Json字符串存储在隐藏的输入字段中。好吧,我可以通过编程方式完成它,但是转发出错了。由于我的字符串适度长,很难逃脱“char为所有名称。请解释它是如何以编程方式工作(阶段1),因为控制台输出看起来相同。
[{"X":0,"Y":0,"W":0,"H":500},{"X":358,"Y":62,"W":200,"H":500}]test2.html:21
[{"X":0,"Y":0,"W":0,"H":500},{"X":358,"Y":62,"W":200,"H":500}]
test2.html:22
PASSED PHASE 1
jquery.min.js:16Uncaught SyntaxError: Unexpected end of input
感谢,
BSR。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE HTML><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Test</title>
<meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
</head>
<body>
<input type="hidden" id="jsondata" />
<input type="hidden" id="jsondata2" value="[{"X":0,"Y":0,"W":0,"H":500},{"X":358,"Y":62,"W":200,"H":500}]"/>
<script >
$(document).ready(function() {
myItems = [{"X":0,"Y":0,"W":0,"H":500},
{"X":358,"Y":62,"W":200,"H":500}]
console.log(JSON.stringify(myItems));
$("#jsondata").val(JSON.stringify(myItems));
console.log(document.getElementById("jsondata").value);
console.log("PASSED PHASE 1");
var obj = jQuery.parseJSON($("#jsondata2").val());
console.log(obj.length);
console.log("PASSED PHASE 2");
});
</script>
</body>
</html>
编辑:
以下代码有效..不确定它是否正确。所以将一个很好的解释标记为答案。感谢。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE HTML><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Test</title>
<meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
</head>
<body>
<input type="hidden" id="jsondata" />
<input type="hidden" id="jsondata2" value='[{"X":0,"Y":0,"W":0,"H":500},{"X":358,"Y":62,"W":200,"H":500}]'/>
<script >
$(document).ready(function() {
myItems = [{"X":0,"Y":0,"W":0,"H":500},
{"X":358,"Y":62,"W":200,"H":500}]
console.log(JSON.stringify(myItems));
$("#jsondata").val(JSON.stringify(myItems));
console.log(document.getElementById("jsondata").value);
console.log("PASSED PHASE 1");
var obj = jQuery.parseJSON($("#jsondata2").val());
console.log($("#jsondata2").val());
console.log(obj[0].H);
console.log("PASSED PHASE 2");
});
</script>
</body>
</html>
答案 0 :(得分:8)
你可以做这样的事情,但是非常糟糕,HTML:
<textarea id="jsondata" sytle="display:none"></textarea>
和JS
$(function(){
var myItems = [{"X":0,"Y":0,"W":0,"H":500}, {"X":358,"Y":62,"W":200,"H":500}]
$("#jsondata").val(JSON.stringify(myItems));
});
答案 1 :(得分:7)
<input type="hidden" id="jsondata2" value="[{"X":0,"Y":0,"W":0,"H":500},{"X":358,"Y":62,"W":200,"H":500}]"/>
不正确。在值字符串中使用单引号'
代替双引号"
来修复它(或转义"
)
<input type="hidden" id="jsondata2" value="[{'X':0,'Y':0,'W':0,'H':500},{'X':358,'Y':62,'W':200,'H':500}]"/>
答案 2 :(得分:3)
html标记无效...如果你对第一个html(非工作的)运行html验证器,你会发现错误...
答案 3 :(得分:2)
查看有关报价的答案。
原因很简单 - 当你有这个代码时:
[sometag someattr="qwerty"123":cxzcxz"/]
浏览器将其理解为:
[sometag someattr="qwerty"/]
最后一部分(123“:cxzcxz”)只是作为一个垃圾扔掉了。因此,对于您的具体案例,parseJSON尝试使用这个:
[{
但你认为它是一个完整的字符串,如:
[{"X":0,"Y":0,"W":0,"H":500},{"X":358,"Y":62,"W":200,"H":500}]
在服务器端输入隐藏字段值之前,应该对json进行编码。
答案 4 :(得分:0)
尝试下面的代码,看起来你有一个数组而不是我的对象。
myItems = { data : [{"X":0,"Y":0,"W":0,"H":500},
{"X":358,"Y":62,"W":200,"H":500}] };