【发布时间】:2015-11-18 08:36:24
【问题描述】:
我在 javascript 文件中有以下代码:
if(dojo.byId('WC_selectedColorNumber') == null && this.defaultColor != null)
{
dijit.byId('WC_color_selection').domNode.style.display = 'block';
dojo.html.set(dojo.query(".message__button .add"), "Add product with only base color " + this.defaultColor + "?");
var userResponse = true;
dojo.connect(WC_add_color_yes, "onclick", function(evt){
userResponse = true;
});
dojo.connect(WC_add_color_no, "onclick", function(evt){
userResponse = false;
});
//var userResponse = confirm("Add product with only base color " + this.defaultColor + "?");
//I WANT TO WAIT HERE FOR THE RESPONSE
if(userResponse == false) //if user clicks Cancel or 'no', display a message and leave the function.
{
alert("Remember to select a color before adding to cart."); //should be a tooltip/popup (not javascript alert) with the same message
return; //return so item doesn't get added to cart
}
}
首先,这段代码背后的逻辑是正确的,并且在使用javascript确认时运行良好。
到目前为止,一切都出现并正确显示,并且单击按钮执行正确的操作(如果我将 console.log 放入 onclick dojo 事件中,当我单击按钮时它们确实会打印到控制台)。但是,程序不会等待响应,而是在看到用户输入之前继续执行 dojo.connect 方法。
我需要它等到按下是或否按钮,但我不知道该怎么做。我试过使用
while(userResponse == null);
但是a)这通常是一个糟糕的想法,并且b)无论如何它都没有用。
如何让我的代码等到用户单击两个按钮之一?
【问题讨论】:
标签: javascript jquery events button dojo