加载脚本后,我有以下 JavaScript:
var current_selected_note = $('#new_note');
current_selected_note.addClass('hover active');
$('#note-item-lists').on('click', '.list-group-item', function () {
//removes the hover color from the previous selected
current_selected_note.removeClass('hover active');
// sets the currently selected equal to the selected note
current_selected_note = $(this);
// adds the hover active to the currently selected
current_selected_note.addClass('hover active');
//adds the title of the currently selected to the title input field
$('#txt_new_note_title').val($(this).find('Strong').text());
selected_note_id = $(this).get(0).id;
getNote(selected_note_id);
load_comments(selected_note_id);
});
$( "#note-item-lists").find('li').first().trigger( "click" );
现在加载此内容后,我单击其中一个具有以下 javascript 的按钮:
$('#note-item-lists').on('click','.close',function(){
var r = confirm('Are you sure you wish to delete "'+$(this).next('div').find('.new_note_title').text()+'" ?')
if(r == true){
deleteNote($(this));
$( "#note-item-lists").find('li').first().click();
}
})
function deleteNote(button){
var id = button.closest('li').get(0).id;
$.ajax({
type: 'POST',
url: '/solo/ajax_delete',
dataType: 'json',
data: {
id: id
},
success: function (data) {
}
});
button.closest('li').remove();
}
当发生这种情况时(我对其进行调试)并且事件函数首先被调用一次(正确添加类),但随后立即再次发生。
有人尝试过这个吗?
最佳答案
试试这个,它会调用一次。
$('#note-item-lists .close').on('click',function(){
alert("Hello");
});
关于javascript - jquery onclick 运行两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21151888/