在事件jquery中插入一个函数

insert a function inside an event jquery

本文关键字:一个 函数 事件 jquery 插入      更新时间:2023-11-11

我正试图在这个jquery事件中插入一个函数。这是事件

$(":checkbox").on('change', function() {
        $.fn.myFunction();
})

这是myFunction

    $.fn.myFunction = function() {
            alert('ciao');
            if ($(this).is(':checked')) {
            InputInserireInput = $(this).parent().parent().parent().find('.inserirePrezzoDiv');
            $(this).closest('div').parent().parent().find('select').prop('value','0');
            $(this).find('button').css('background-color','#c31432')
            var checkbox = $(this);
            InputInserireInput.removeClass('hidden');
            var checkboxSelected = $(this).attr('id');                            
            var nomeServizio = ($(this).next('label').text());                   
            $(this).closest('div').parent().parent().find('.titoloPiega').css('color', 'black')
            $(this).closest('div').parent().parent().find('button').css('margin-top', -50)
            var titleLabel = $(this).closest('div');                         
            var titleSelect = $(titleLabel).parent().parent().find('.titoloPiega'); 
            var option1 = $(titleLabel).parent().parent().find('.option1');
            var option2 = $(titleLabel).parent().parent().find('.option2');
            var selectOption = ($(titleLabel).parent().parent().find('select'));   
            var selectOptionID = ($(titleLabel).parent().parent().find('select').attr('id')); 
            $(selectOption).on('change', function () {
                var selectedOption = $(selectOption).prop('value');
                if (selectedOption == 1 && checkbox.is(':checked')) {
                    InputInserireInput.addClass('hidden');
                    option2.addClass('hidden');
                    option1.removeClass('hidden');
                } 
                if (selectedOption == 2 && checkbox.is(':checked')) {
                    InputInserireInput.addClass('hidden');
                    option1.removeClass('hidden');
                    option2.removeClass('hidden');
                }      
               if (selectedOption == 0 && checkbox.is(':checked')) {
                    InputInserireInput.removeClass('hidden');
                    option1.addClass('hidden');
                    option2.addClass('hidden');
                }              
            });
        } else  {
            $('.inserirePrezzoDiv').addClass('hidden');       
            $(this).closest('div').parent().parent().find('.titoloPiega').css('color', '#a5a6a7');
            $(this).closest('div').parent().parent().find('select').prop('value','0');        
            $(this).closest('div').parent().parent().find('.option1').addClass('hidden');
            $(this).closest('div').parent().parent().find('.option2').addClass('hidden');
            $('.bottoneCss6').css('margin-bottom', 0)
        }   
    };

第一个allert有效,但随后所有if语句i thik都与函数之前声明的事件不匹配。有人能帮我吗?

尝试替换:

$(":checkbox").on('change', function() {
    $(this).myFunction()
})

您将把this(当前复选框)作用域传递给myFunction