2013-06-05 37 views
0

每次用戶啓動應用程序時,應用程序會要求用戶選擇「第一個」或「第二個」。 如果用戶的選擇是「第一」,則顯示消息「測試1」,或者如果用戶的選擇是「第二」,則消息是「測試2」。消息對話框用戶選擇Metro應用程序 - JavaScript

不管我選擇的消息總是「測試1」。

我的代碼有什麼問題?

我怎樣才能使它工作?

var msg = new Windows.UI.Popups.MessageDialog("Choose first or second.", "Test"); 

// Add commands and set their command handlers 
msg.commands.append(new Windows.UI.Popups.UICommand("First", commandInvokedHandler)); 

msg.commands.append(new Windows.UI.Popups.UICommand("Second", commandInvokedHandler)); 

// Set the command that will be invoked by default 
msg.defaultCommandIndex = 1; 

// Set the command to be invoked when escape is pressed 
msg.cancelCommandIndex = 1; 

// Show the message dialog 
msg.showAsync(); 

if (msg = 1) { 
    // First Choice 
    var button = document.getElementById("button"); 
    applyButton.addEventListener("click", buttonFirst, false); 
} else { 
    // Second Choice 
    var button = document.getElementById("button"); 
    applyButton.addEventListener("click", buttonSecond, false); 
} 

function buttonFirst(eventInfo) { 
    var greetingString = "TEST 1"; 
    document.getElementById("first").innerText = greetingString; 
} 

function buttonSecond(eventInfo) { 
    var greetingString = "TEST 2"; 
    document.getElementById("second").innerText = greetingString; 
} 

回答

0

if(msg = 1) {應該if(msg == 1) {否則你將它設置爲1,然後檢查它。

編輯:

在仔細檢查,,還有這裏的更大的問題。爲什麼要分開添加eventListeners? The spec for the UICommand class指出第二個參數是回調。所以,而不是你創建按鈕的方式,爲什麼不做:

msg.commands.append(new Windows.UI.Popups.UICommand("First", buttonFirst)); 

msg.commands.append(new Windows.UI.Popups.UICommand("Second", buttonSecond)); 
+0

它仍然無法正常工作。 – kalpetros

+0

請參閱我的編輯。不知道這是否會解決它,但我有一種感覺可能有所幫助 – n00dle

+0

我試過了,它不起作用。我所做的是我用'buttonFirst'和'buttonSecond'替換了'commandInvokedHandler',並且我還從事件處理程序中刪除了'if else'。我不確定我是否做得對。 – kalpetros

相關問題