Code is taking effect when it shouldn't be

后端 未结 2 1803
我寻月下人不归
我寻月下人不归 2021-01-25 14:18

I have a function where if the user clicks on a button, it will display a value in the textbox and it will perform a trigger to another function after a \'#btn\' button is click

2条回答
  •  失恋的感觉
    2021-01-25 14:54

    The problem comes from the fact taht you're setting the data-attribute to false on the $('#mainNumberAnswerTxt') but you're checking against the inputs (this in the click).

    One solution would be to do the following test if ($('#mainNumberAnswerTxt').attr('data-ignore') != true)

    EDIT

    To ensure that the ignore process execute only once (when the grid is reloaded after the modal), you'll have to change what's inside the block after the test:

    if ($('#mainNumberAnswerTxt').data('ignore') != true) {
        $('.answertxt', context).val(context.find('.answerBtnsOn').length > 0 ? context.find('.answerBtnsOn').length : 0);
    } else {
        /*reset the ignore flag*/
        $('#mainNumberAnswerTxt').data('ignore',false);
    }
    

    EDIT2

    The main problem is that your reaffecting the $('#mainNumberAnswerTxt').val(numberAnswer) after setting it. Your trying to ignore it through the ignoreattribute.

    What i would adivse, would be to remove every occurence to that attribute and to change the following function

    function addwindow(questionText,textWeight,numberAnswer,gridValues,reply,btn) {
        var answers = $.map(btn.split(''),function(chr){ return "#answer"+chr; }).join(', ');
        if($(plusbutton_clicked).attr('id')=='mainPlusbutton') {
            var myNumbers = {};
            myNumbers["A-C"] = "3";
            myNumbers["A-D"] = "4";
            myNumbers["A-E"] = "5";
            myNumbers["A-F"] = "6";
            myNumbers["A-G"] = "7";
            gridValues = myNumbers[gridValues];
            $('#mainNumberAnswerTxt').show();
            $('#mainNumberAnswerTxt').val(numberAnswer).data('ignore',true);
            $('#mainGridTxt').val(gridValues).parent().append($('.optionTypeTbl'));
            $('#btn'+gridValues).trigger('click');
            $('.answers.answerBtnsOn').removeClass('answerBtnsOn').addClass('answerBtnsOff');
            $(answers).addClass("answerBtnsOn").siblings().addClass('answerBtnsOff');
        }
        $.modal.close();
        return false;
    } 
    

    to :

    function addwindow(questionText,textWeight,numberAnswer,gridValues,reply,btn) {
        var answers = $.map(btn.split(''),function(chr){ return "#answer"+chr; }).join(', ');
        if($(plusbutton_clicked).attr('id')=='mainPlusbutton') {
            var myNumbers = {};
            myNumbers["A-C"] = "3";
            myNumbers["A-D"] = "4";
            myNumbers["A-E"] = "5";
            myNumbers["A-F"] = "6";
            myNumbers["A-G"] = "7";
            gridValues = myNumbers[gridValues];
            $('#mainNumberAnswerTxt').show();
            $('#mainGridTxt').val(gridValues).parent().append($('.optionTypeTbl'));
            $('#btn'+gridValues).trigger('click');
            /* moved the following line below the click simulation, 
               hence its value will be changed regardless of what happened during the click simulation*/
            $('#mainNumberAnswerTxt').val(numberAnswer);
            $('.answers.answerBtnsOn').removeClass('answerBtnsOn').addClass('answerBtnsOff');
            $(answers).addClass("answerBtnsOn").siblings().addClass('answerBtnsOff');
        }
        $.modal.close();
        return false;
    } 
    

    In order to modify the value after the visual changes took place.

提交回复
热议问题