如何使用jQuery同时向多个不同的按钮添加确认对话框
How to add a confirm dialogue to many different buttons at once with jQuery?
假设我有这个HTML。。。
<button class="btn-remove-this">Remove this</button>
<button class="btn-remove-that">Remove that</button>
<button class="btn-remove-some-other-thing">Remove some other thing</button>
<!-- and many more 'Remove ...' buttons -->
以及这个JavaScript。
$(function() {
$('.btn-remove-this').click(function() {
functionWhichRemovesThis();
}
$('.btn-remove-that').click(function() {
functionWhichRemovesThat();
}
$('.btn-remove-some-other-thing').click(function() {
functionWhichRemovesSomeOtherThing();
}
// and many more click handlers
});
现在,我想在删除所有这些内容之前,用确认对话框提示用户。有没有一种方法可以做到这一点,而不在每个单击处理程序中添加和调用confirm
?
我有一些想法,比如在所有不同的按钮上添加一个类(比如btn-remove
),然后添加一个单击处理程序,看起来像这样:
$('.btn-remove').click(function() {
if (confirm('Are you sure you want to remove this?')) {
// execute the body of the "more specific" click handler
} else {
// prevent the body of the "more specific" click handler from executing
}
}
您可以为不同的按钮编写不同的点击处理程序,并调用公共检查函数。您想要调用的函数有一个参数,点击ok时可以调用该回调函数。
$('.btn-remove-this').click(function() {
check(functionWhichRemovesThis);
});
$('.btn-remove-that').click(function() {
check(functionWhichRemovesThat);
});
$('.btn-remove-some-other-thing').click(function() {
check(functionWhichRemovesSomeOtherThing);
});
function check(callback){
if (confirm('Are you sure you want to remove this?')) {
callback();
} else {
// prevent the body of the "more specific" click handler from executing
}
}
这也是一种方式。这样您就不必对代码进行太多修改。
我建议您在这里使用data-*
:
<button class="btn-remove" data-func="functionWhichRemovesThis">Remove this</button>
<button class="btn-remove" data-func="functionWhichRemovesThat">Remove that</button>
<button class="btn-remove" data-func="functionWhichRemovesSomeOtherThing">Remove some other thing</button>
现在,在你的js代码中,你可以这样做:
var removeUtil = {
functionWhichRemovesThis : function(){},
functionWhichRemovesThat : function(){},
functionWhichRemovesSomeOtherThing : function(){}
};
$('.btn-remove').click(function() {
if (confirm('Are you sure you want to remove this?')) {
var removeIt = $(this).data('func');
removeUtil[removeIt];
}
});
下面是我如何做到这一点的示例:
<button class="btn-remove btn-remove-some-other-thing">Remove something</button>
<button data-callback="App.remove.that" data-confirmText="Remove that?" class="btn-remove btn-remove-some-other-thing">Remove that</button>
<button data-callback="App.remove.this" data-confirmText="Remove this?" class="btn-remove btn-remove-some-other-thing">Remove this</button>
<script type="text/javascript">
var App = {};
App.remove = {};
// create an object to define different callbacks, outside of document(ready) so you an access it anywhere else you want.
App.remove['this'] = {
'yes': function () {
console.log('this.yes')
},
'no': function () {
console.log('this.no')
}
};
App.remove['that'] = {
'yes': function () {
console.log('that.yes')
},
'no': function () {
console.log('that.no')
}
};
$(document).ready(function () {
$('.btn-remove').click(function () {
var callback = {};
if (typeof $(this).attr('data-callback') !== 'undefined') {
// get the callback object of current button.
callback = $(this).attr('data-callback').split('.').reduce(function (obj, i) {
return App.remove[i];
}, App.remove);
}
var confirmText = 'Are you sure you want to remove this?';
if (typeof $(this).attr('data-confirmText') !== 'undefined') {
// set alternate text if needed
confirmText = $(this).attr('data-confirmText');
}
if (confirm(confirmText)) {
if (callback.hasOwnProperty('yes')) {
// do callback if property exists in callback object
callback.yes();
}
} else {
if (callback.hasOwnProperty('no')) {
// do callback if property exists in callback object
callback.no();
}
}
});
});
</script>
相关文章:
- 将按钮添加到新元素中
- 使用加号按钮添加多个文本框,并通过PHP提交
- 复选框/单选按钮-添加所选项目的总价
- 将自定义按钮添加到现有网站
- 单击按钮添加HTML表单
- 将sharethis按钮添加到chrome扩展popup.html
- 从自定义DocuSign按钮添加信封自定义字段
- 我尝试将引导下拉按钮添加到 但下拉菜单不起作用.我该怎么做
- 将按钮添加到跨度
- 使用按钮添加到变量
- 单击禁用的按钮添加 CSS
- 将“下一步”和“上一个”按钮添加到较大的图像
- 将下一个和上一个按钮添加到我的图像滑块
- 按React按钮添加行
- 用jQuery点击按钮添加值
- 如何将下一个和上一个按钮添加到图像galary
- 如何为表行结果的每个按钮添加事件侦听器
- 使用setTimeout为按钮添加冷却时间
- 无法向按钮添加属性
- 如何将我的附加按钮添加到这个jQuery mp3播放器中