Javascript JQuery-无法检测复选框更改事件

Javascript JQuery-无法检测复选框更改事件,javascript,jquery,checkbox,Javascript,Jquery,Checkbox,我目前正在处理以下JQuery: $('table input[type=checkbox][data-tag=entity-value]').on('change', function () { var associatedTextboxID = $(this).attr('id').replace('IsAssociated', 'Value'); var associatedTextbox = $('table input[type=text][id

我目前正在处理以下JQuery:

$('table input[type=checkbox][data-tag=entity-value]').on('change',
    function () {
        var associatedTextboxID = $(this).attr('id').replace('IsAssociated', 'Value');
        var associatedTextbox = $('table input[type=text][id=' + associatedTextboxID + ']');

        if ($(this).prop('checked') && associatedTextbox.val() === "") {
            $(this).prop('border', '1px solid #FF0000');
            $(this).after("<p id='error' class='key-error'>While associated, value is a required field.</p>");
            $('#btnSaveCustomProperty').prop('disabled', true);
            $('#btnSaveCustomPropertyAndContinue').prop('disabled', true);
        }
        else if (!$(this).prop('checked')) {
            $('#error').remove();
            $('#btnSaveCustomProperty').prop('disabled', false);
            $('#btnSaveCustomPropertyAndContinue').prop('disabled', false);
        }
    });
$('table input[type=checkbox][data tag=entity value]')。在('change')上,
函数(){
var associatedTextboxID=$(this).attr('id').replace('IsAssociated','Value');
变量associatedTextbox=$('TableInput[type=text][id='+associatedTextboxID+']');
if($(this.prop('checked')&&associatedTextbox.val()=“”){
$(此).prop('border','1px solid#FF0000');
$(this).在关联时(“

之后,值是必填字段。

”; $('btnSaveCustomProperty').prop('disabled',true); $('btnSaveCustomPropertyAndContinue').prop('disabled',true); } else if(!$(this.prop('checked')){ $(“#错误”).remove(); $('btnSaveCustomProperty').prop('disabled',false); $('btnSaveCustomPropertyAndContinue').prop('disabled',false); } });
当用户更改复选框值时,我尝试执行块。在浏览器中单步浏览代码会发现,当选中(或未选中)复选框时,代码不会执行


是否有更好的方法来处理复选框事件?

复选框是动态生成的吗?我使用模型(.net)在UI中填充文本框和复选框,基于上一页中的选择。您没有提供足够的信息。如果这些是动态创建的元素,请使用.live或确保您的jquery版本高于1.7(自1.7以来,.live已被弃用并捆绑到.on方法中)。一些步骤可能有助于开始确定哪种情况是这样的:打开浏览器控制台并运行以下命令:
$('table input[type=checkbox][data tag=entity value]”)
。如果它返回了某些内容,很好,那么您的选择器是正确的。如果没有返回,那么您的选择器是错误的。如果它确实返回了某些内容,请将
调试器;
放在设置on change侦听器的位置之前。现在在控制台中运行相同的选择器。很可能不会,您需要将其包装在$(文档)中。准备好了吗(function(){})如selten98所述,或者如果表是动态生成的,则使用.on()筛选器参数。嗯,好的……您可以尝试将侦听器更改为:
$('table')。on('change',input[type=checkbox][data tag=entity value]',function(){…})