2012-05-11 61 views
13

不幸的是,文檔Bootbox(http://paynedigital.com/2011/11/bootbox-js-alert-confirm-dialogs-for-twitter-bootstrap)沒有教會如何調用確認對話框。由於它在文檔窗口中總是在頁面加載時出現,出現了什麼問題,因此在點擊刪除按鈕調用時應該會出現。 這是嘗試失敗的代碼。Bootbox確認對話框問題

// show "false" does not work, the confirm window is showing when page is loaded. 
$(function() { 
bootbox.confirm("Confirm delete?", "No", "Yes", function(result) { 
show: false 
});  
}); 

// need show the confirm window when the user click in a button, how to call the confirm window? 

<td><a class="icon-trash" onclick="bootbox.confirm();" href="{% url 'del_setor' setor.id %}" title="Delete"></a></td> 

如何將此Bootbox設置爲僅在單擊刪除按鈕時才顯示? 謝謝!

編輯 - 溶液:現在

$(function() { 
$("a#confirm").click(function(e) { 
    e.preventDefault(); 
    var location = $(this).attr('href'); 
    bootbox.confirm("Confirm exclusion?", "No", "Yes", function(confirmed) { 
     if(confirmed) { 
     window.location.replace(location); 
     } 
    }); 
});  
}); 

,當我點擊 「是」 刪除工作。 我讓插件的開發者把整個樣本放在文檔中,所以用戶不需要在點擊「是」時創建關於如何刪除對象的文章。 此致敬禮。

+0

笑笑 - 是的,這似乎喜歡的事,開發商將需要知道的。 –

回答

6

您可能要檢查此頁面上的「基本使用」中的「確認」鏈接的源:http://bootboxjs.com/

這裏有一個片段:

$("a.confirm").click(function(e) { 
    e.preventDefault(); 
    bootbox.confirm("Are you sure?", function(confirmed) { 
     console.log("Confirmed: "+confirmed); 
    }); 
}); 

假設jQuery是在UI提供您'正在處理,我會避免在您的定位標記中使用onClick屬性。只是我的兩分錢。

+0

好的,謝謝你的回答!現在窗口出現,但是當我點擊「是」時,沒有任何反應,排除不會發生。 此致敬禮。 – LinuxMan

+0

您可能想要檢查JS控制檯以查看是否有錯誤被拋出。沒有看到你正在編寫的代碼,很難說出是什麼原因造成了這個問題。你可以把它扔進JSFiddle嗎? – jstam

+1

代碼發佈後,沒有必要在jsfiddle中發佈,因爲我只有這個代碼才能顯示。在那裏我沒有任何東西要移除。 發生了什麼很簡單,我點擊「是」,儘管在firebug中顯示「remove:true」,但對象並未被刪除。沒有錯誤消息出現在螢火蟲,根本不會刪除對象。問候。 – LinuxMan

0
$(document).on("click", ".confirm-modal", function(e) { 
    e.preventDefault(); 
    bootbox.confirm("Are You sure?", function(result) { 
     if(result) { 
      top.location.href = e.target.href; 
     } 
    }); 
}); 
+2

對於這樣一個遲到的答案,你應該添加解釋。 – manuell

3

我需要的,但我卻變化不大幾件事......看看...

添加此功能爲您的全球「的jQuery就緒」功能,這樣的:

$(document).on("click", "[data-toggle=\"confirm\"]", function (e) { 
    e.preventDefault(); 
    var lHref = $(this).attr('href'); 
    var lText = this.attributes.getNamedItem("data-title") ? this.attributes.getNamedItem("data-title").value : "Are you sure?"; // If data-title is not set use default text 
    bootbox.confirm(lText, function (confirmed) { 
     if (confirmed) { 
      //window.location.replace(lHref); // similar behavior as an HTTP redirect (DOESN'T increment browser history) 
      window.location.href = lHref; // similar behavior as clicking on a link (Increments browser history) 
     } 
    }); 
}); 

而且只需添加一些「數據 - *屬性」你的按鈕或鏈接這樣的:

<a class="btn btn-xs btn-default" data-toggle="confirm" data-title="Do you really want to delete the record 123?" href="/Controller/Delete/123"><span class="fa fa-remove"></span> </a> 

所以......這樣你就可以有AP ersonalized question ..這對你的用戶來說更直觀...

你喜歡嗎?

觀看演示:jsfiddle