通过jquery获取json编码的值

时间:2015-07-14 03:34:13

标签: jquery json

HTML:



  <input type="text" class="hide text" id="scharge" value="[{" servicecharge":{"id":"1","pieces":"2","discount":"50"}},{"servicecharge":{"id":"3","pieces":"3","discount":"60"}},{"servicecharge":{"id":"4","pieces":"4","discount":"70"}},{"servicecharge":{"id":"6","pieces":"1","discount":"0"}}]"="" style="display: none;">

如何获取id="scharge"的价值?

我的jquery是:

  var sc = $('#scharge').val();
//  var scs = $.parseJSON(sc);

  console.log(sc);

但它显示以下响应:

[{

2 个答案:

答案 0 :(得分:1)

将双引号"替换为单引号'

 <input type="text" class="hide text" id="scharge" value='[{" servicecharge":{"id":"1","pieces":"2","discount":"50"}},{"servicecharge":{"id":"3","pieces":"3","discount":"60"}},{"servicecharge":{"id":"4","pieces":"4","discount":"70"}},{"servicecharge":{"id":"6","pieces":"1","discount":"0"}}]"' style="display: none;">

var sc = $('#scharge').val();

console.log(sc);

Jsfiddle

答案 1 :(得分:0)

将JSON编码数据换成单引号,将其分配给<input>值:

<input type="text" class="hide text" id="scharge" value='[{"servicecharge":{"id":"1","pieces":"2","discount":"50"}},{"servicecharge":{"id":"3","pieces":"3","discount":"60"}},{"servicecharge":{"id":"4","pieces":"4","discount":"70"}},{"servicecharge":{"id":"6","pieces":"1","discount":"0"}}]' style="display: none;">
Single quote                                            ^ and...>>>                                                                                                                                                                                                                         here ^