当单选框被选中和取消选中时,我希望能获取其值。我目前正在使用codeigniter框架。当点击单选框时,我可以获取其值,但是当点击未选中的框时,我无法获取其值。请帮帮我!我的代码如下。
<table>
<thead>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</thead>
<tbody>
<tr>
<td>
<input name="feature" type="checkbox" id="10" value="10" />
</td>
<td>
<input name="feature" type="checkbox" id="11" value="11" />
</td>
<td>
<input name="feature" type="checkbox" id="12" value="12" />
</td>
<td>
<input name="feature" type="checkbox" id="13" value="13" />
</td>
</tr>
</tbody>
</table>
<script type="text/javascript">
$("input:checkbox").change(function() {
var someObj = {};
someObj.fruitsGranted = [];
var feature , key;
$("input:checkbox").each(function() {
if ($(this).is(":checked")) {
key = someObj.fruitsGranted.push($(this).attr("feature"));
someObj.fruitsGranted.push($(this).attr("id"));
feature = $(this).attr("id");
//key = $(this).attr("feature");
}
});
alert("GRANTED: " + feature + " ok " + key);
});
</script>
$("input:checkbox").change(function() { if (!$(this).is(":checked") { $(this).attr("id") ...
- undefined$("input:checkbox").each(
,你的键/特征将始终来自于最后一个复选框。移除.each
并使用$(this).is(":checked")
来判断当前的复选框是否被选中。 - undefinedchange
事件对于选中/取消选中都会触发,并且this
将是复选框 - 所以$(this).val()
是其值,上面的链接和注释显示它是否被选中。 - undefined