Javascript 选择“上一个”;这";要素

Javascript 选择“上一个”;这";要素,javascript,jquery,Javascript,Jquery,我有一段javascript代码,我试图从中获得前面声明的元素。目前我有两个onclick函数 $('#formIngredient').on("click", '.newIngredient', function() { value = $(this).attr('data-name'); $(this).attr('data-isactive', 'true'); $('#newIngredientInput').val(value); $('#newIng

我有一段javascript代码,我试图从中获得前面声明的元素。目前我有两个onclick函数

$('#formIngredient').on("click", '.newIngredient', function() {
    value = $(this).attr('data-name');
    $(this).attr('data-isactive', 'true');

    $('#newIngredientInput').val(value);
    $('#newIngredient').modal('show')

    $('#createNewIngredient').click(function() {
        inputName = $('#newIngredientInput').val();
        inputCategory = $('#newIngredientCategory').val();

        var newIngredient = $.ajax({
            type: "GET",
            url: '/content/includes/ajax/createNewIngredient.php',
            data: {name: inputName, id_category: inputCategory}
        });

        newIngredient.done(function(result) {
            //PAI.showPage(PAI['PAGE']);
            $(this).parent().replaceWith('Hello');
            $('#newIngredient').modal('hide');
        });
    });
});
我正在尝试将前面的元素用于此代码

$(this).parent().replaceWith('Hello');
有什么想法吗?

使用以下代码:

$(this).parent().prev().replaceWith('Hello');
$(此)
的范围似乎是您的问题。。。您需要更正代码

$('#createNewIngredient').click(function() {
    var self = this; // This is the #createNewIngredient element

    inputName = $('#newIngredientInput').val();
    inputCategory = $('#newIngredientCategory').val();

    var newIngredient = $.ajax({
        type: "GET",
        url: '/content/includes/ajax/createNewIngredient.php',
        data: {name: inputName, id_category: inputCategory}
    });

    newIngredient.done(function(result) {
        //PAI.showPage(PAI['PAGE']);
        // $(this).parent().replaceWith('Hello'); // $(this) scope is lost to current function... so
        $(self).parent().replaceWith('Hello'); // This is now the scope you need
        $('#newIngredient').modal('hide');
    });
});

我将介绍您的代码逻辑(嵌套的单击事件),但您的问题似乎与“this”引用(作用域)有关:

这是最简单的方法,但最好使用闭包:

(function(self){
var newIngredient = $.ajax({
                type: "GET",
                url: '/content/includes/ajax/createNewIngredient.php',
                data: {name: inputName, id_category: inputCategory}
            });

            newIngredient.done(function(result) {
                //PAI.showPage(PAI['PAGE']);
                $(self ).parent().replaceWith('Hello');
                $('#newIngredient').modal('hide');
            });

})(this);

什么?您可以显示您的标记。您是否知道每次单击“FormingCredit”时您都在为“CreateNewComponent”on click事件创建一个新的处理程序?这些嵌套的单击处理程序看起来有问题(但与ajax回调中的
问题无关)。太棒了。这就成功了。谢谢。没问题,很乐意帮忙。我会避免使用
self
作为变量名。谢谢。再好不过了:)
(function(self){
var newIngredient = $.ajax({
                type: "GET",
                url: '/content/includes/ajax/createNewIngredient.php',
                data: {name: inputName, id_category: inputCategory}
            });

            newIngredient.done(function(result) {
                //PAI.showPage(PAI['PAGE']);
                $(self ).parent().replaceWith('Hello');
                $('#newIngredient').modal('hide');
            });

})(this);