2015-04-29 46 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; 
相关问题