1
我想從我的JSON數組中選中複選框,但它只返回值爲「1」的複選框;我錯過了什麼?從JSON中選擇複選框
這裏是我的小提琴http://jsfiddle.net/geegirls3/acx1yLex/
<div id="checkboxes">
<input type="checkbox" name="qcol2" value="1" > Sarah
<br />
<input type="checkbox" name="qcol2" value="9" > Sundae
<br />
<input type="checkbox" name="qcol2" value="10" > Summer
<br />
<input type="checkbox" name="qcol2" value="11" > GeeZee
<br />
<input type="checkbox" name="qcol2" value="12" > Husband
<br />
</div>
$(document).ready(function() {
var initValues = [{my_id: "10"}, {my_id: "11"}, {my_id: "12" }];
var myjson = JSON.stringify(initValues);
//alert(myjson);
$('#checkboxes').find(':checkbox[name^="qcol2"]').each(function() {
$(this).prop("checked", ($.inArray($(this).val(), myjson) != -1));
//alert(myjson);
});
});
它的工作謝謝。根據你的幫助,我改變了我的代碼。我的小提琴https://jsfiddle.net/geegirls3/70jd4nv8/ – GeeGirls3