2012-08-06 102 views
1

ajax看起來像jQuery的:點擊關閉

// send the data to the server using .ajax() or .post() 
    $.ajax({ 
     type: 'POST', 
     url: 'addVideo', 
     data: { 
      video_title: title, 
      playlist_name: playlist, 
      url: id 
      // csrfmiddlewaretoken: '{{ csrf_token }}', 
     }, 
     done: notify('success', 'video saved successfully'), 
     fail: notify('error', 'There were some errors while saving the video. Please try in a while') 
    }); 

notify看起來像

function notify(notify_type, msg) { 
    var alerts = $('#alerts'); 
    alerts.addClass('alert'); 
    alerts.append('<a class="close" data-dismiss="alert" href="#">×</a>'); 
    if (notify_type == 'success') { 
     alerts.addClass('alerts-success').append(msg).fadeIn('fast'); 
    } 
    if (notify_type == 'failure') { 
     alerts.addClass('alerts-error').append(msg).fadeIn('fast'); 
    } 
} 
  • 當警報刪除div標籤當我保存按鈕點擊我獲得成功的消息爲

視頻成功保存X(十字標誌)

  • 我點擊交叉,現在通知了
  • 當我再次保存點擊按鈕,沒有任何反應,我看到螢火蟲抱怨 No elements were found with the selector: "#alerts"

  • 我的猜測是點擊十字標記完全從DOM中刪除div="alerts"標記。這是對的嗎?

問題 - 我怎樣才能得到正確的行爲。點擊在十字標記刪除通知格,點擊按鈕來創建創建通知格再次

+0

哪裏是你當你點擊你的警報的緊密聯繫代碼插件解決這一問題? – MrOBrian 2012-08-06 21:47:12

+0

我什麼也沒做,我依賴'alerts.append('×');' – fullstackcrash 2012-08-06 21:51:20

+0

可能你會停止在每個問題中告訴twitter bootstarap?它現在看起來像廣告。並讓ppl忽略問題,即使它簡單js問題 – 2012-08-06 21:57:13

回答

1

其實根據this當您單擊x插件刪除div中,當您嘗試選擇div再次使用var alerts = $('#alerts');關閉後,一旦div不存在於dom中。

插件的一部分,負責密切/刪除元素

function removeElement() { 
    $parent 
    .trigger('closed') 
    .remove() 
} 

$.support.transition && $parent.hasClass('fade') ? 
    $parent.on($.support.transition.end, removeElement) : 
    removeElement() 

嘗試創建每次(ID爲alerts)動態股利和要在其中放置其追加到身體,它可能工作。

+0

感謝您的幫助,我得到了正確的方向和http://stackoverflow.com/questions/10082330/dynamically-create-bootstrap-alerts-box-through-javascript – fullstackcrash 2012-08-06 22:52:20

+0

歡迎您,並感謝您的鏈接:-) – 2012-08-06 23:02:13

+0

這裏有另一種方法:http://stackoverflow.com/questions/20494887/my-bootstrap-alert-will-not-display-after-being-closed/20495097#20495097 – Sk8erPeter 2014-06-10 22:48:21

1

正如謝赫Heera說,你可以通過改變

function removeElement() { 
    $parent 
    .trigger('closed') 
    .remove() 
} 

function removeElement() { 
    $parent 
    .trigger('closed') 
    .hide() 
} 
+1

所以你建議黑客「核心」?餿主意。 – Sk8erPeter 2014-06-10 22:42:39