0

动态生成的输入按钮有时会调用该函数(该函数显示在同一页面上,在 之外$(document).ready()),但有时会给出Typeerror该函数不存在。
shiftRight()中,我正在创建一个按钮shiftLeftButton,并在单击它时,我正在shiftLeft()使用参数调用函数。shiftLeftButtons不止一个。 有时会shiftLeftButton使用一些参数调用函数,但有时不会。

我试图调试这个并搜索了这个问题,但没有得到解决方案。

function shiftRight(leftButtonNo) {
    //Creating a hidden field in the form
    if(!$("#innerQuestionListDiv"+leftButtonNo).doesExist()){
        if(!$("#clonedDiv"+leftButtonNo).doesExist()){
            $('#TestSetFormId').append('<input type="hidden" name="questionIdsForTestSet" value="'+ leftButtonNo+'" id="questionIdForTestSet'+ leftButtonNo+'" />');



                   ***//Creating shift left button 
            var $shiftLeftButton= $('<input/>').attr({ 
                type: 'button', name:'shiftLeft', 
                id:'shiftLeftId'+leftButtonNo , 
                value:'<--'+leftButtonNo+'',
                onclick:'shiftLeft('+leftButtonNo+')'
                });***

                   //Getting clone of the div
            $cloneQuestionDiv=$("#div"+leftButtonNo).clone();

                    ***//Appending it to a cloned div
            $cloneQuestionDiv.append($shiftLeftButton);***

            $cloneQuestionDiv.attr('id', 'clonedDiv'+leftButtonNo);
            $cloneQuestionDiv.prependTo('#existingQuestionDiv');
            $("#list"+leftButtonNo).hide();
        }else{
            alert("You already added this question");
            $("#list"+leftButtonNo).hide();
        }   
    }else{
        alert("Question already exist into the Test Set");
        $("#list"+leftButtonNo).hide();
        return;
    }
}

 *function shiftLeft(rightButtonNo){
    //Removing hidden field in the form
    $("#questionIdForTestSet"+rightButtonNo).remove();

    $("#clonedDiv"+rightButtonNo).remove();

    if($("#list"+rightButtonNo).doesExist()){
        $("#list"+rightButtonNo).show();    
    }
    $(this).hide();
}* 
4

1 回答 1

0

尝试这个:

function shiftRight(leftButtonNo) {

    //Creating a hidden field in the form
    if(!$("#innerQuestionListDiv" + leftButtonNo).doesExist()) {

        if(!$("#clonedDiv"+leftButtonNo).doesExist()) {

            var button = $('<input type="hidden" name="questionIdsForTestSet" value="'+ leftButtonNo+'" id="questionIdForTestSet'+ leftButtonNo+'" />');
            $('#TestSetFormId').append(button);


            var 
                $cloneQuestionDiv = $("#div"+leftButtonNo).clone(),
                $list = $("#list"+leftButtonNo);

            var $shiftLeftButton = (function(leftButtonNo, button, $cloneQuestionDiv, $list) {

                var newButton = $('<input/>').attr({ 
                    type: 'button', 
                    name:'shiftLeft', 
                    id:'shiftLeftId'+leftButtonNo , 
                    value:'<--' + leftButtonNo
                 });

                newButton.bind('click', function(event) {
                    shiftLeft(leftButtonNo, button, $cloneQuestionDiv, $list, event);
                });

                return newButton;

            }(leftButtonNo, button, $cloneQuestionDiv, $list));


            $cloneQuestionDiv.append($shiftLeftButton);

            $cloneQuestionDiv.attr('id', 'clonedDiv'+leftButtonNo);
            $cloneQuestionDiv.prependTo('#existingQuestionDiv');

            $list.hide();

        }else{
            alert("You already added this question");
            $("#list"+leftButtonNo).hide();
        }   
    }else{
        alert("Question already exist into the Test Set");
        $("#list"+leftButtonNo).hide();
        return;
    }
}

function shiftLeft(rightButtonNo, questionIdForTestSet, cloneQuestionDiv, list, event){

    //Removing hidden field in the form
    $(questionIdForTestSet).remove();

    $(cloneQuestionDiv).remove();

    if($(list).doesExist()) {
        $(list).show();    
    }

    $(this).hide();
}
于 2013-09-10T15:54:10.780 回答