2014-01-16 17 views
0

我有,當我的腳本加載以下JavaScript:的jQuery的onclick運行兩次

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(); 
} 

當發生這種情況(我調試它)和第一次調用事件函數(正確添加類),然後再次立即發生。

以前有人試過嗎?

回答

2

試試這個嘗試,它會調用一次。

$('#note-item-lists .close').on('click',function(){ 
      alert("Hello");  
}); 
+0

是的它只做過一次這是什麼意思?爲什麼不添加這個類呢? –

0

使用.off()

$('#note-item-lists').on('click', '.list-group-item', function() { 
    $(this).off(); //add this here 
+0

可悲的是,這並沒有訣竅:S –