我有一段JavaScript代碼,我試圖去聲明前面的元素。目前我在兩個相互之間有兩個onclick函數。選擇上一個「this」元素
$('#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');
任何想法?
以前是什麼?你可以顯示你的標記。 – 2013-05-02 11:42:19
您是否知道每次單擊'formIngredient'時您在點擊事件中創建'createNewIngredient'的新處理程序? – 2013-05-02 11:42:22
這些嵌套的點擊處理程序看起來有問題(但與您在ajax回調中的「this」問題無關)。 – Bergi 2013-05-02 11:45:35