2016-09-19 179 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>