我对如何使用其中的值填充一些复选框有一些问题,newArray[]
代码如下:
Html:
<form id="myForm">
<div id ="checkboxes">
<table>
<tr>
<td>
<label class="checkbox">
<input type="checkbox" name="my_opt[]" value="1">
Total
</label>
</td>
<td>
<label class="checkbox">
<input type="checkbox" name="my_opt[]" value="2">
Male
</label>
</td>
</tr>
<tr>
<td>
<label class="checkbox">
<input type="checkbox" name="my_opt[]" value="3">
Female
</label>
</td>
<td>
<label class="checkbox">
<input type="checkbox" name="my_opt[]" value="4">
Age
</label>
</td>
</tr>
<tr>
<td>
<label class="checkbox">
<input type="checkbox" name="my_opt[]" value="5">
Score
</label>
</td>
</tr>
</table>
</div>
<input id='my_opt' type='hidden' name='my_opt[]' />
</form>
脚本:
<script>
//checkboxes
var check_box_values = $('#myForm [type="checkbox"]:not(:checked)').map(function () {
return parseInt(this.value);
}).get();
(...)
j = j + 1;
//creating new button
$("#save").append(
$("<input type=button id=saved"+j+">").click(function() {
(...)
}).text("Chart"+j)
);
//change the checkboxes to new values from array
var newArray = [];
newArray[4] = check_box_values; //i want that to be [4]
//click on new button
$('#saved'+j).click(function() {
$('#checkboxes').val(newArray[4]);
});
</script>
这不起作用,并且错误似乎在 $('#checkboxes').val(newArray[4]);
. 我想要的是根据newArray[4];
例如数据检查复选框。如果newArray[4] = [2,3,4]
我错过了语法:s你能帮我吗?