动态生成的输入按钮每次都不调用函数

时间:2013-09-10 15:37:39

标签: javascript jquery

动态生成的输入按钮有时会调用该函数(该函数显示在$(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();
}* 

1 个答案:

答案 0 :(得分: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();
}