2014-09-07 2 views
3

jQuery 변수를 bootbox.js 확인 상자에 전달할 수 있습니까?어떻게하면 Bootbox에 변수를 전달할 수 있습니까?

"제품 이름은"변수처럼
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(); 

이 가능 뭔가가 여기에

코드인가?

업데이트 : 아래 답변에 감사드립니다!

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

}); 

}); 

가 어떻게 "이"콜백 내에서 다시 정의 할

: 나는 이런 식으로 뭔가가 있다면

....이 질문에 태킹? "foo"는 bootbox를 참조하기 때문에 undefined를 반환합니다.

답변

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

내 실례로 좋았습니다. 고맙습니다! – LBF

+0

$ (this)를 사용하면 같은 메소드가 콜백 내에서 작동하지 않는 이유를 알고 있습니까? $ (this)는 이제 bootbox를 대신 가리 키기 때문에 정의되지 않은 것 같습니다. 콜백 내에서 "this"를 재정의하는 방법이 있습니까? – LBF

+0

외부 참조 콜백에 참조를 저장하고 해당 참조를 사용해야합니다. –

1

당신이 콜백 함수에서이 변수로 작업해야하는 경우, u는 "전역"으로 만 전달할 수 있지만 사용자 호출 확인과 콜백 실행 사이의 시간에 다른 스크립트에서이를 바꿀 수 있습니다 (또는 역순으로). 따라서 충돌을 방지하기 위해 메서드를 사용하여 개체를 만들고 변수 및 변수를 가져와야합니다. 변수를 잠 그거나 잠금 해제하는 차단 메커니즘 tatus를 set/get 이벤트에 적용하고 모든 이벤트를 비동기로 실행하십시오.

var _this = this; 
confirm({ 
    title: "Menu discard", 
    message: 'Are you sure?', 
    callback: function(resolve) { 
     if(resolve) { 
      // _this is accessible 
     } 
    } 
}); 
관련 문제