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

Is it possible to pass a jQuery variable into a bootbox.js confirm box?Here is the code:bootbox.confirm

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
        }
    }
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744369570a4570877.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信