简体   繁体   中英

insert a function inside an event jquery

i'm trying to insert a function inside this jquery event. This is the event

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

and this is 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)
        }   
    };

The first allert work but then all the if statement i thik doesn' match with the event declared before the function. Can anyone help me?

try by replacing :

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

you will pass the this (current checkbox) scope to myFunction

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM