javascript - jquery onclick 运行两次

标签 javascript jquery

加载脚本后,我有以下 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/

相关文章:

javascript - 我怎样才能让 AngularJS 绑定(bind)与花式框对话框一起工作?

javascript - 单击按钮时触发 Ctrl+z 和 Ctrl+y 按键

javascript - knockout 类型错误 : m. apply is not a function

jquery 添加或切片

javascript - 根据表单元素的选定值动态更改表单字段标签的技术

javascript - 在没有 jQuery 的 Javascript 中,我要查找一个数组中的任何值是否存在于一个单独的数组中

javascript - AngularJS指令: Using ng-switch breaks transclusion

javascript - 模式不适用于 ie8

javascript - 在 JavaScript 的 span 标签中获取没有任何嵌套元素的文本值

javascript - jQuery Mobile Ajax 导航超时