2017-07-29 41 views
0

我解析數據陣列與jQuery parseJSON一個問題,返回:jQuery的解析數據數組意外標記

SyntaxError:Unexpected token in JSON at position 1

var selected = $('input[type=radio][name=packs]:checked'); 
 
gf_allowed = $.parseJSON(selected.data('gf_allowed'));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input type="radio" name="packs" value="361" data-price="22,00" data-desc=" 2 GGFF + BUGGIE(22.00EUR pax)" data-gf_exact="True" data-gf_allowed="[&quot;2&quot;, &quot;4&quot;]" checked="true">

我在做什麼錯?

+0

你需要的,如果可以共享整個js代碼/文件。 – roray

回答

0

你的代碼的問題是因爲jQuery已經認識到屬性中保存的值是JSON,因此將它解析爲一個對象給你。您再次解析的呼叫是導致問題的原因。

要解決此問題只是刪除$.parseJSON()電話:

var selected = $('input[type=radio][name=packs]:checked'); 
 
var gf_allowed = selected.data('gf_allowed'); // jQuery has already parsed this for you 
 

 
console.log(gf_allowed); 
 
console.log(gf_allowed[1]); // = 4
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input type="radio" name="packs" value="361" data-price="22,00" data-desc=" 2 GGFF + BUGGIE(22.00EUR pax)" data-gf_exact="True" data-gf_allowed="[&quot;2&quot;,&quot;4&quot;]" checked="true">

+0

它完美的作品!謝謝 –