简体   繁体   English

使用ajax无法动态添加输入字段时,jQuery单击浏览

[英]JQuery to click on Browse when Input field is added dynamically with ajax not working

I am having issues having jQuery-click on the browse button. jQuery-click浏览按钮时出现问题。 Here is a sample of my code. 这是我的代码示例。 My code basically dynamically adds an input field (in ajax) and I want the input field to be "Clicked" on after it is added but it doesn't work properly. 我的代码基本上是动态地添加一个输入字段(以ajax格式),并且我希望在添加输入字段后将其“单击”,但是它不能正常工作。 It works if it is ran twice but it is always the previous input browse field that is clicked on, not the current. 如果运行两次,它将起作用,但是总是单击前一个输入浏览字段,而不是当前。

 $( document ).delegate( ".add_picture", "click", function() {
    //$('.add_picture').click(function(){
        //Check and see if there is a selected question
        if (isAnythingHighlighted()){
            var assessmentID = $('#assessment_name_input').attr("data-assessmentid");
            var questionID = $('.highlight').attr("data-questionid");
            var order = $(".highlight ul li").length;
            var addQuestionItem=true;
            var questionItemTypeID = 3; //3 for picture
            if (debug) 
            {
               alert(questionID);
            } 
            jQuery.ajax({
                type: "POST", // HTTP method POST or GET
                url: "create_assessment_response.php", //Where to make Ajax calls
                dataType:"text", // Data type, HTML, json etc.
                data:{
                        addQuestionItem:addQuestionItem, 
                        questionID:questionID, 
                        questionItemTypeID:questionItemTypeID, 
                        order:order, 
                        assessmentID:assessmentID
                }, //Form variables
                success:function(response){
                        response = response.trim();
                        //Code for the Picture li
                        var li = 
                                $("\<li class='pictureItem' data-questionItemUniqueID='" + response + "'>\
                                    <a class='deleteQuestionItem'>\
                                    <i class='remove_icon fa fa-remove'></i></a>\
                                    <h2>Image</h2>\
                                    <div class='wrap'>\
                                    <form action='ajaxupload.php' method='post' enctype='multipart/form-data'>\
                                    <img style='display:none' class='loader' src='loader.gif' alt='Loading....' title='Loading....' />\
                                    <input id='browse"+response+"' class='uploadImage' type='file' accept='image/*' name='image' />\
                                    <input class='imageSubmit' type='submit' value='Upload' data-questionItemUniqueID='" + response + "'>\
                                    <div class='preview' style=''></div>\
                                    </form>\
                                    </div>\
                                    </li>");
                        //Add the li to the highlighted question
                        $(".highlight ul").append(li);
                        makeSubListSortable();
                        $.getUpdatedLog();
                        //$(".highlight ul").children('li').find('input[type=file]').trigger("click"); this won't work. Auto open file browse.

                        // $(".highlight ul").children('li:first').find('input[type=file]').click();

                   },
                   error:function (xhr, ajaxOptions, thrownError){
                         alert(thrownError);
                   }
        });
        //end ajax 
}


$(".highlight ul").children('li.pictureItem:last').find("input[type=file]").trigger("click");
});
//example appended Li code: <li class="pictureItem" data-questionitemuniqueid="7515">            <a class="deleteQuestionItem">            <i class="remove_icon fa fa-remove"></i>            </a>            <h2>Image</h2>            <div class="wrap">            <form action="ajaxupload.php" method="post" enctype="multipart/form-data">            <img style="display:none" class="loader" src="loader.gif" alt="Loading...." title="Loading....">            <input id="browse7515" class="uploadImage" accept="image/*" name="image" type="file">            <input class="imageSubmit" value="Upload" data-questionitemuniqueid="7515" type="submit">            <div class="preview" style=""></div>            </form>            </div>            </li>

I found a temporary solution. 我找到了一个临时解决方案。 Using this setTimeout seems to work. 使用此setTimeout似乎有效。

           setTimeout(function(){
  $(".highlight ul").children('li.pictureItem:last').find("input[type=file]").trigger("click");
}, 300);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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