最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can you pass a variable to Bootbox confirm? - Stack Overflow

programmeradmin8浏览0评论

Is it possible to pass a jQuery variable into a bootbox.js confirm box?

Here is the code:

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

Where "productName" is a variable like:

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

Is something like this possible?

UPDATE: Thanks for the answer below!

Tacking onto this question.... if I had something like this:

$('#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!
   }
}

});

});

How do I redefine "this" inside the callback? "foo" returns undefined because it's referring to bootbox now.

Is it possible to pass a jQuery variable into a bootbox.js confirm box?

Here is the code:

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

Where "productName" is a variable like:

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

Is something like this possible?

UPDATE: Thanks for the answer below!

Tacking onto this question.... if I had something like this:

$('#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!
   }
}

});

});

How do I redefine "this" inside the callback? "foo" returns undefined because it's referring to bootbox now.

Share Improve this question edited Jun 30, 2015 at 14:54 Siguza 24k6 gold badges55 silver badges98 bronze badges asked Sep 7, 2014 at 18:19 LBFLBF 1,2052 gold badges18 silver badges42 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

Yes you can, you just have to concatenate the value with the message string :

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",

or more clean:

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",

If you need to work with this variable in callback function, u can pass it by making "global", but in time between user call confirm and start to execute callback some other script can change it (or reverse), so for prevent conflicts desirable to create some object with methods to set, get variables and blocking mechanism that lock/unlock variable status on set/get events and execute all events not async.

var _this = this;
confirm({
    title: "Menu discard",
    message: 'Are you sure?',
    callback: function(resolve) {
        if(resolve) {
            // _this is accessible
        }
    }
});
发布评论

评论列表(0)

  1. 暂无评论