2015-04-29 47 views
1

溝通我在HTA,看起來像這樣一個javascript:模態對話框不與主HTA窗口

var result = null; 
window.showModalDialog("dialog.hta", window, "dialogHeight:300px; dialogWidth:300px"); 
alert(result); 

dialog.hta:

<html> 
<head> 
    <title>Dialog box</title> 
    <meta http-equiv="MSThemeCompatible" content="yes"/> 
</head> 
<body style="background:#F0F0F0"> 
    <select id="colors"> 
      <option selected>Red</option> 
      <option>Blue</option> 
      <option>Green</option> 
      <option>Yellow</option> 
    </select><br/> 
    <script type="text/javascript"> 
      function ok(){ 
       window.dialogArguments.result = colors.getElementsByTagName("option")[colors.selectedIndex].innerHTML; 
       window.close(); 
      } 
    </script> 
    <button onclick="ok()">OK</button> 
    <button onclick="window.close()">Cancel</button> 
</body> 
</html> 

的問題是,當我按確定主要HTA窗口中的alert(result)始終表示爲空,即使我在模式對話框中單擊確定按鈕。 我該怎麼做才能說明按下確定按鈕時用戶在列表中選擇的選項,按下取消按鈕時是否爲空?

回答

1

這是怎麼了模態對話框作品:

在主應用程序:

// Call a dialog, and store the returned value to a variable 
var result = showModalDialog(path, argument, options); 

在對話框關閉:

// Set the returnValue 
var elem = document.getElementById("colors"); 
window.returnValue = elem[elem.selectedIndex].text; 
top.close(); 

的對話框中,可以設置returnValue後在關閉對話框後從result中讀取它。

option元素在舊IE中沒有innerHTML,因此您必須改用text屬性。您還可以將value屬性添加到select元素,然後以簡單方式創建返回值:

window.returnValue = document.getElementById('colors').value; 
相關問題