如何使用jquery选择来自ajax响应的元素?

如何使用jquery选择来自ajax响应的元素?,jquery,ajax,Jquery,Ajax,我想选择一个来自第一个ajax响应的html元素,作为GET参数在第二个ajax请求中使用。但是,由于第二个ajax请求参数是在加载页面时加载的,所以第二个ajax请求无法获得来自第一个ajax响应的新值。我如何处理这个问题?以下是我在代码方面的问题: JS: HTML: 它总是第一位,然后第二位吗?如果是这样,您可以在成功中启动第二个:secondCallpassYourNewDataAsParam;作为一个函数,我不完全确定我是否理解您试图做什么,但我怀疑您只需要使用事件委派来绑定到动态加载

我想选择一个来自第一个ajax响应的html元素,作为GET参数在第二个ajax请求中使用。但是,由于第二个ajax请求参数是在加载页面时加载的,所以第二个ajax请求无法获得来自第一个ajax响应的新值。我如何处理这个问题?以下是我在代码方面的问题:

JS:

HTML:


它总是第一位,然后第二位吗?如果是这样,您可以在成功中启动第二个:secondCallpassYourNewDataAsParam;作为一个函数,我不完全确定我是否理解您试图做什么,但我怀疑您只需要使用事件委派来绑定到动态加载的元素。请看,它始终是第一个ajax。但可能是第一个ajax,也可能是第二个ajax。首先,ajax上传图像。第二,ajax确实删除了图像。所以这取决于用户的选择。能否将$$imgOutput imgName更改为$imgOutput imgName?它们之间有什么区别?它们是一样的,而且是错误的。
$(document).ready(function() {
    $("#imageInput").change(function(){
        $("#addText").submit();
    });

    var optionsUpload = { 
            url: 'bilesenler/ajaxUploadImage.php',
            beforeSubmit: beforeSubmit,  // pre-submit callback 
            success: afterAdding,  // post-submit callback 
            resetForm: false        // reset the form after successful submit 
        }; 


    // $("#imgOutput .removeInlineImage") object will come from 
    // first ajax response. Because of this I cannot select this 
    // future element "now" like in the following line. Any suggestion???

    $("#imgOutput .removeInlineImage").on("click", function(){
        var optionsRemove = {
             // $("#imgOutput #imgName") object will come from 
             // first ajax response. I cannot select the future
             // element "now" like in the following.Any suggestion?

            url: 'bilesenler/ajaxRemoveImage.php=imgName=' + $("$imgOutput #imgName"),
            beforeSubmit: beforeRemoving, 
            success: afterRemoving, 
            resetForm: false        
        };
    });


     $('#addText').submit(function() { 
            if(!imgDelete)
                $(this).ajaxSubmit(optionsUpload); 
            else{
                $(this).ajaxSubmit(optionsRemove);
            }

            return false; 
        }); 
});
<form action="../adminPaneli/yaziEkle.php" method="POST" name="addText" id="addText" enctype="multipart/form-data">

<!-- There are some codes -->

<input type="file" name="inlineImage" id="imageInput"> <!-- Look the id ! -->

<div id="imgOutput"></div> <!-- Look the id ! -->

<!-- There are some codes -->

</form>
<?php
    echo '<div id="'.$image_name_only.'" class="previewImage">';
    echo '<img class="thumbImage" src="../kitaplik/resimler/upload/'.$new_file_name.'">';
    echo '<br/>';
    echo '<input type="text" id="imgName" class="imgName" value="'. $new_file_name . '">';

    // The important element in this file is the following line. 
    // Look the class name : removeInlineImage. It is used in jquery code above.
    echo '<span class="removeInlineImage">&nbsp;&nbsp;X</span>'; 
    echo '</div>';
?>
$(document).ready(function() {
    $("#imageInput").change(function(){
        $("#addText").submit();
    });

    var optionsUpload = { 
            url: 'bilesenler/ajaxUploadImage.php',
            beforeSubmit: beforeSubmit,  // pre-submit callback 
            success: afterAdding,  // post-submit callback 
            resetForm: false        // reset the form after successful submit 
        }; 




    // $("#imgOutput .removeInlineImage") object will be able to
    // be catched with ajaxSuccess event.

    $(document).ajaxSuccess( function() {

        $("#imgOutput .removeInlineImage").on("click", function(){  


            selectedImageName = $(this).prev().val();

            optionsRemove = {
                url: 'bilesenler/ajaxRemoveImage.php?imgName=' + selectedImageName,
                beforeSubmit: beforeRemoving,
                success: afterRemoving, 
                resetForm: false  
            };

            imgDelete = true;
            $("#addText").submit();
        });
    });

    $('#addText').submit(function() { 
        if(!imgDelete)
            $(this).ajaxSubmit(optionsUpload); 
        else{
            $(this).ajaxSubmit(optionsRemove);
        }

        return false; 
    }); 
});