wawan Setiyawan wawan Setiyawan - 4 months ago 20
JSON Question

jquery error when split

I've got an error when I split for checkbox.



var ukuran = data['model'].ukuran_model;
var cek = ukuran.split(",").join('], [value='),
$inputs = $('input[name^=ukuran]');
$inputs.filter('[value=' + cek + ']').prop('checked', true);

<div class="row">
<div class="col-md-1 ukuran">
<input class="ukuran_input" type="checkbox" name="ukuran[]" value="XS">XL</div>
<div class="col-md-1 ukuran">
<input class="ukuran_input" type="checkbox" name="ukuran[]" value="S">S</div>
<div class="col-md-1 ukuran">
<input class="ukuran_input" type="checkbox" name="ukuran[]" value="M">M</div>
<div class="col-md-1 ukuran">
<input class="ukuran_input" type="checkbox" name="ukuran[]" value="L">L</div>
<div class="col-md-1 ukuran">
<input class="ukuran_input" type="checkbox" name="ukuran[]" value="XL">XL</div>
<div class="col-md-1 ukuran">
<input class="ukuran_input" type="checkbox" name="ukuran[]" value="2L">2L</div>
<div class="col-md-1 ukuran">
<input class="ukuran_input" type="checkbox" name="ukuran[]" value="3L">3L</div>
<div class="col-md-1 ukuran">
<input class="ukuran_input" type="checkbox" name="ukuran[]" value="4L">4L</div>
<div class="col-md-1 ukuran">
<input class="ukuran_input" type="checkbox" name="ukuran[]" value="5L">5L</div>
<div class="col-md-1 ukuran">
<input class="ukuran_input" type="checkbox" name="ukuran[]" value="Small">Small</div>
</div>
<div class="row">
<div class="col-md-2 ukuran">
<input class="ukuran_input" type="checkbox" name="ukuran[]" value="BigSize">BigSize</div>
<div class="col-md-2 ukuran">
<input class="ukuran_input" type="checkbox" name="ukuran[]" value="ExtraBig">ExtraBig</div>
<div class="col-md-2 ukuran">
<input class="ukuran_input" type="checkbox" name="ukuran[]" value="5/6">5/6</div>
<div class="col-md-2 ukuran">
<input class="ukuran_input" type="checkbox" name="ukuran[]" value="7/8">7/8</div>
<div class="col-md-2 ukuran">
<input class="ukuran_input" type="checkbox" name="ukuran[]" value="9/10">9/10</div>
</div>
<div class="row">
<div class="col-md-2 ukuran">
<input class="ukuran_input" type="checkbox" name="ukuran[]" value="11/12">11/12</div>
<div class="col-md-2 ukuran">
<input class="ukuran_input" type="checkbox" name="ukuran[]" value="SS">SS</div>
<div class="col-md-2 ukuran">
<input class="ukuran_input" type="checkbox" name="ukuran[]" value="S/M">S/M</div>
<div class="col-md-2 ukuran">
<input class="ukuran_input" type="checkbox" name="ukuran[]" value="L/XL">L/XL</div>
</div>

<!-- begin snippet: js hide: false console: true babel: false -->





This is the data from the JSON response:

{
"bahan": [{
"id_bahan": "6",
"nm_bahan": "Combed Sakura",
"stok_bahan": "3",
"nm_jenis": "Katun Sp 08",
"nm_warna": "merah",
"no": 1
}, {
"id_bahan": "3",
"nm_bahan": "Bhn Jaket 02",
"stok_bahan": "3",
"nm_jenis": "Katun Sp 08",
"nm_warna": "hijau",
"no": 2
}],
"model": {
"id_model": "4",
"nm_model": "Zn 225 W",
"ukuran_model": "XS,S,M,L,XL,2L,3L,4L,5L,Small,BigSize,ExtraBig,5\/6,7\/8,9\/10,11\/12,SS,S\/M,L\/XL",
"foto_model1": "9_Cardigan-Muslimah-Zenitha-Modis-DAyw7Gafw.jpg",
"foto_model2": "",
"foto_model3": "",
"keterangan_model": "cddcscs"
},
"detail": [{
"id_detail": "8"
}, {
"id_detail": "7"
}]
}


its show error in google chrome development (
concole.log()
), this say error :


jQuery-2.1.4.min.js:2 Uncaught Error: Syntax error, unrecognized expression: [value=XS], [value=S], [value=M], [value=L], [value=XL], [value=2L], [value=3L], [value=4L], [value=5L], [value=Small], [value=BigSize], [value=ExtraBig], [value=5/6], [value=7/8], [value=9/10], [value=11/12], [value=SS], [value=S/M], [value=L/XL]


How can I fix it? So far so good when I input checkbox just four checkbox. But, when i input many checkbox, i've error !

Answer

Add doble quotes. find fiddle here

var ukuran = 'XL,S,M,L';
var cek = ukuran.split(",").join('"], [value="');
var inputs = $('input[name^=ukuran]');
inputs.filter('[value="' + cek + '"]').prop('checked', true);