3

是否可以将 jQuery 变量传递到 bootbox.js 确认框?

这是代码:

bootbox.confirm({
message: 'Are you sure you want to remove [productName]?',
callback: function(result) {
    console.log(result);
},
title: "You can also add a title",
});

其中“productName”是一个变量,例如:

var productName = $('#product_name').val();

这样的事情可能吗?

更新:感谢下面的回答!

解决这个问题......如果我有这样的事情:

$('#product_name1 a.trash').click(function(event){

event.stopImmediatePropagation();           
event.preventDefault();

var foo = $(this).parent().attr('id');  // #product_name1

alert(foo); // correct!

bootbox.confirm({
message: 'Are you sure you want to remove [productName]?',
callback: function(result) {
   if (confirmed) 
    alert(foo); // undefined!
   }
}

});

});

如何在回调中重新定义“this”?"foo" 返回 undefined 因为它现在指的是 bootbox。

4

2 回答 2

2

是的,您可以,您只需将值与消息字符串连接起来:

var productName = $('#product_name').val();

bootbox.confirm({
message: 'Are you sure you want to remove'+productName,
callback: function(result) {
    console.log(result);
},
title: "You can also add a title",

或更干净:

var productName = $('#product_name').val();
var Message = 'Are you sure you want to remove'+productName;

    bootbox.confirm({
    message: Message ,
    callback: function(result) {
        console.log(result);
    },
    title: "You can also add a title",
于 2014-09-07T18:26:35.027 回答
2

如果你需要在回调函数中使用这个变量,你可以通过设置“全局”来传递它,但是在用户调用确认和开始执行回调之间的时间里,一些其他脚本可以改变它(或反转),所以为了防止冲突是可取的使用设置方法创建一些对象,获取变量和阻塞机制,在设置/获取事件上锁定/解锁变量状态并执行所有非异步事件。

var _this = this;
confirm({
    title: "Menu discard",
    message: 'Are you sure?',
    callback: function(resolve) {
        if(resolve) {
            // _this is accessible
        }
    }
});
于 2014-09-26T08:42:44.527 回答