2016-09-19 180 views
0

假設有一個HTML頁面,當我點擊一個按鈕,如何利用中出現的元素我讀:」當我點擊一個按鈕

enter image description here

我需要處理,當我點擊‘OK’

是HTML代碼:

<button data-bb-handler="confirm" type="button" class="btn btn-primary">OK</button> 

這是我的jQuery代碼:

(function() { 

     $(document).on('click','button[data-bb-handler="confirm"][type="button"]',function(){ 
      console.log("MAREEEEEEEEEEEEEEEEE "+$(this).length); 
     }); 

}()); 

它不起作用。任何人都可以幫助我?其他一些方法?

+1

你需要什麼是什麼呢?在點擊「確定」之後顯示課程或者執行類似於從數據庫中刪除帖子的操作? –

+0

是的,我需要刪除db中的一些元素 – Ashe

+0

你可以使用sql查詢嗎?!像這樣的代碼:'SELECT teamname FROM usersystem WHERE teamname ='「。$ teamname。」''|如果你能告訴我舉一個例子 –

回答

0

這段jQuery代碼應工作:

$('button[data-bb-handler="confirm"][type="button"]').click(function(){ 
    console.log("MAREEEEEEEEEEEEEEEEE "+$(this).length); 
}); 

讓我知道,如果它不工作。勾選此Working Codepen

+0

它不起作用! :( – Ashe

+0

檢查上述鏈接的工作代碼,並檢查其行爲是否如預期 –

+0

@NikhilNanjappa你的代碼鏈接是完全不同的東西..錯誤的鏈接? – cjmling

0

我建議使用Bootbox JS。它基於Bootstrap佈局。

你可以做到這一點,你需要爲下面提到

bootbox.confirm("Are you sure?", function(result) { 
    // write your code what you need to handle on OK button 
}); 
0

您可以使用事件show.bs.modal

// 
 
// On modal show 
 
// 
 
$(document).on('show.bs.modal', function (e) { 
 
    // 
 
    // attach the click event for OK button 
 
    // 
 
    $('button[data-bb-handler="cancel"][type="button"]').on('click', function (e) { 
 
    console.log("CANCEL BUTTON PRESSED"); 
 
    }); 
 

 
    // 
 
    // attach the click event for CANCEL button 
 
    // 
 
    $('button[data-bb-handler="confirm"][type="button"]').on('click', function (e) { 
 
    console.log("OK BUTTON PRESSED"); 
 
    }); 
 
}); 
 

 
$(document).ready(function() { 
 
    $('#myBtn').on('click', function (e) { 
 
    bootbox.confirm("Are you sure you wish to discard this post?", function() { 
 
    }); 
 
    }).trigger('click'); 
 
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> 
 
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script> 
 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
 
<script src="https://github.com/makeusabrew/bootbox/releases/download/v4.4.0/bootbox.min.js"></script> 
 

 
<button id="myBtn" type="button" class="btn btn-primary">Click Me</button>

這是bootbox 。確認消息。對於deatils,你可以參考Bootbox.js

當您單擊確定或取消該對話框DOM元素從文檔中刪除。

所以,你必須使用回調函數:

$(document).on('click.bootbox', function(e, result){ 
 
    console.log("Pressed "+ result); 
 
}); 
 

 
bootbox.confirm("Are you sure you wish to discard this post?", function(result) { 
 
    $(document).trigger('click.bootbox', result); 
 
});
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> 
 
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script> 
 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
 
<script src="https://github.com/makeusabrew/bootbox/releases/download/v4.4.0/bootbox.min.js"></script>