我是编程新手并且坚持到了一步。我正在使用一个API,它返回一个数组中的多个值。我以包含复选框的形式获取这些值,我只想检查那些由API返回的复选框。意味着它就像一个票务系统。例如我有seatdata数组,我想检查那些在php数组中有id的复选框。
<?php
$seatdata = array("A1", "A6", "A10","A11");
?>
<table>
<tr>
<td><input type='checkbox' name='seatdata[]' value='0|12' id="A11" /><label for="A11">A11</label></td>
<td><input type='checkbox' name='seatdata[]' value='0|11' id="A10"/> <label for="A10">A10</label></td>
<td><input type='checkbox' name='seatdata[]' value='0|10' id="A9" /><label for="A9">A9</label></td>
<td><input type='checkbox' name='seatdata[]' value='0|9' id="A8" /><label for="A8">A8</label></td>
<td><input type='checkbox' name='seatdata[]' value='0|6' id="A7" /><label for="A7">A7</label></td>
<td><input type='checkbox' name='seatdata[]' value='0|5' id="A6" /><label for="A6">A6</label></td>
<td><input type='checkbox' name='seatdata[]' value='0|4' id="A5" /><label for="A5">A5</label></td>
<td><input type='checkbox' name='seatdata[]' value='0|3' id="A4" ?> /><label for="A4">A4</label></td>
<td><input type='checkbox' name='seatdata[]' value='0|2' id="A3" /><label for="A3">A3</label></td>
<td><input type='checkbox' name='seatdata[]' value='0|1' id="A2" /><label for="A2">A2</label></td>
<td><input type='checkbox' name='seatdata[]' value='0|0' id="A1"/><label for="A1">A1</label></td>
</tr>
我也试过下面的代码,但它不能正常工作
<html>
<head></head>
<body>
<script type="javascript">
var data = ["A1", "A6", "A10","A11"];
$.each(data,function(i,v){
$('#'+v).prop('checked',true)
});
</script>
<table>
<tr>
<td>
<input type='checkbox' name='seatdata[]' value='0|12' id="A11" />
<label for="A11">A11</label>
</td>
<td>
<input type='checkbox' name='seatdata[]' value='0|11' id="A10" />
<label for="A10">A10</label>
</td>
<td>
<input type='checkbox' name='seatdata[]' value='0|10' id="A9" />
<label for="A9">A9</label>
</td>
<td>
<input type='checkbox' name='seatdata[]' value='0|9' id="A8" />
<label for="A8">A8</label>
</td>
<td>
<input type='checkbox' name='seatdata[]' value='0|6' id="A7" />
<label for="A7">A7</label>
</td>
<td>
<input type='checkbox' name='seatdata[]' value='0|5' id="A6" />
<label for="A6">A6</label>
</td>
<td>
<input type='checkbox' name='seatdata[]' value='0|4' id="A5" />
<label for="A5">A5</label>
</td>
<td>
<input type='checkbox' name='seatdata[]' value='0|3' id="A4" />
<label for="A4">A4</label>
</td>
<td>
<input type='checkbox' name='seatdata[]' value='0|2' id="A3" />
<label for="A3">A3</label>
</td>
<td>
<input type='checkbox' name='seatdata[]' value='0|1' id="A2" />
<label for="A2">A2</label>
</td>
<td>
<input type='checkbox' name='seatdata[]' value='0|0' id="A1" />
<label for="A1">A1</label>
</td>
</tr>
</table>
</body>
</html>
答案 0 :(得分:2)
您可以尝试in_array
<input type='checkbox' name='seatdata[]' value='0|12' id="A11" <?php if(in_array("A11", $seatdata)){echo "checked";}?> />