Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在jquery中获取复选框的检查状态_Jquery_Html_Checkbox - Fatal编程技术网

如何在jquery中获取复选框的检查状态

如何在jquery中获取复选框的检查状态,jquery,html,checkbox,Jquery,Html,Checkbox,我有一个复选框,如下所示 <input id='showDeletedQuestionsId' name='showDeletedQuestions' type='checkbox' style='margin-right: 3px' />"" 但是,我注意到,即使选中复选框,它也总是返回false。我想知道如何在jquery中获取检查状态。您没有正确使用ID,showDeletedQuestions SID已定义,并且使用了showDeletedQuestions 使用 作为替代

我有一个复选框,如下所示

<input id='showDeletedQuestionsId' name='showDeletedQuestions' type='checkbox' style='margin-right: 3px' />""

但是,我注意到,即使选中复选框,它也总是返回false。我想知道如何在jquery中获取检查状态。

您没有正确使用ID,
showDeletedQuestions SID
已定义,并且使用了
showDeletedQuestions

使用

作为替代方案,您可以使用


您的代码中有一个错误。 “#”选择器仅用于Id字段。但您在选择中使用了“name”属性。按如下所示修改您的代码,它将正常工作

var showDeletedCheckbox = $('#showDeletedQuestionsId').is(':checked');
供参考

您的id与它们不匹配,它将起作用

$('#showDeletedQuestionsId').change(function () {
    var showDeletedCheckbox = $('#showDeletedQuestionsId').is(':checked');

    if (showDeletedCheckbox) {
        alert('checked')
    } else {
        alert('not checked')
    }
})

您能显示整个代码吗?是的,因为您的代码应该运行良好。请尝试
var showDeletedCheckbox=$(“#showDeletedQuestionsId”)。是(“:checked”)
showDeletedQuestions SID
showDeletedQuestions
不匹配示例我只是显示他的代码工作正常问题是他的id不匹配
var showDeletedCheckbox = $('#showDeletedQuestionsId').prop('checked');
var showDeletedCheckbox = $('#showDeletedQuestionsId').is(':checked');
<input id='showDeletedQuestionsId' name='showDeletedQuestions' type='checkbox' style='margin-right: 3px' />
var showDeletedCheckbox = $('#showDeletedQuestions').is(':checked');
$('#showDeletedQuestionsId').change(function () {
    var showDeletedCheckbox = $('#showDeletedQuestionsId').is(':checked');

    if (showDeletedCheckbox) {
        alert('checked')
    } else {
        alert('not checked')
    }
})