我创建了以下jQuery脚本来检查是否单击了“chkboxAdd”复选框。
(function ( $ ){
$.fn.myfunction = function(){
$("input[type='checkbox']").click(function()
{
if($(this).attr('name') == 'chkboxAdd' && $(this).attr('checked') == true )
{
alert('Checkbox add is checked!');
}
else
{
alert('Checkbox add is NOT checked!');
}
});
};
})(jQuery);
然而,在单击“chkboxAdd”复选框后,警告提示始终使用else子句而不是if子句并提示:
Checkbox add is NOT checked!
我已将相关的HTML代码段包含在内以供参考。提前谢谢。
<script>
$(document).ready(function()
{
$('#chkBoxTable').myfunction();
} );
</script>
<table id="chkBoxTable" cellpadding="10" cellspacing="1">
<thead>
<tr>
<th><strong>Page ID</strong></th>
<th><strong>Page Name</strong></th>
<th><strong>Action</strong></th>
</tr>
</thead>
<?php
<tbody>
<tr>
<td><?php echo $row["PAGEID"]; ?></td>
<td><?php echo $row["PAGENAME"]; ?></td>
<td><input type="checkbox" name="chkboxAdd" value="add">Add</td>
</tr>
</tbody>
</table>
答案 0 :(得分:1)
$(this).attr('checked')
应该是
$(this).prop('checked')
因为您需要属性checked
,而不是属性。获取属性将确定复选框是否具有checked
的实际属性,该属性不会从常规用户输入更改。
示例,如果页面以选中的复选框开始,那么$(this).attr('checked')
将为真,但如果用户取消选中该框,则它仍然等于true。