DIV CSS 佈局教程網

JavaScript 彈出子視窗並把數值回傳給父視窗
編輯:關於JavaScript     

以 showModalDialog 搭配 window.returnValue,可以做出「父視窗開啟選擇視窗、子視窗回傳所選值」的標準互動。

父視窗頁面

<html xmlns="https://www.w3.org/1999/xhtml">
<head runat="server">
  <title>First</title>
</head>
<script type="text/javascript">
function ShowDialog(Url) {
    var iWidth = 560;                                   // 模態視窗寬度
    var iHeight = 300;                                  // 模態視窗高度
    var iTop = (window.screen.height - iHeight - 100) / 2;
    var iLeft = (window.screen.width - iWidth) / 2;
    var returnValue = window.showModalDialog(Url, "newwindow",
        "dialogHeight:" + iHeight + "px; dialogWidth:" + iWidth + "px; " +
        "toolbar:no; menubar:no; scrollbars:no; resizable:no; " +
        "location:no; status:no; left:200px; top:100px;");
    document.getElementById("TextBox1").innerText = returnValue;
}
</script>
<body>
  <form id="form1" runat="server">
    <input id="Button1" type="button" value="button" onclick="ShowDialog('./second.aspx')" />
    <div><asp:TextBox ID="TextBox1" runat="server" Width="67px"></asp:TextBox></div>
  </form>
</body>
</html>

子視窗頁面

子視窗的重點是設定 window.returnValue,然後關閉自己,父視窗的 showModalDialog 呼叫就會拿到這個值並繼續往下執行。

<html xmlns="https://www.w3.org/1999/xhtml">
<head runat="server">
  <title>second</title>
</head>
<script language="javascript">
  function closeDiag() {
    window.opener = null;
    window.close();
  }

  function OK() {
    var Value = document.getElementsByName("myRadio");
    for (var i = 0; i < Value.length; i++) {
      if (Value[i].checked) {
        window.returnValue = Value[i].value;   // 回傳選中的值
        window.close();
      } else {
        document.getElementById("Lab_Info").innerText = "沒有選中項,請選擇!";
      }
    }
  }
</script>
<body>
  <form id="form1" runat="server">
    <div>
      <asp:DataGrid ID="dgSjygl" runat="server" AllowPaging="True"
        AutoGenerateColumns="False" BackColor="White" Font-Size="9pt"
        PageSize="5" Width="100%" Height="127px"
        OnItemDataBound="dgSjygl_ItemDataBound">
        <PagerStyle Mode="NumericPages" />
        <AlternatingItemStyle BackColor="Gainsboro" />
        <ItemStyle BackColor="#EEEEEE" ForeColor="Black" HorizontalAlign="Center" />
        <Columns>
          <asp:TemplateColumn>
            <HeaderTemplate><label>選擇</label></HeaderTemplate>
            <ItemTemplate>
              <input type="radio" id="myRadio" name="myRadio"
                value='<%# DataBinder.Eval(Container.DataItem, "id") %>'>
            </ItemTemplate>
            <HeaderStyle Font-Bold="True" HorizontalAlign="Center" Width="60px" />
            <ItemStyle HorizontalAlign="Center" />
          </asp:TemplateColumn>
          <asp:BoundColumn DataField="ID" HeaderText="編號">
            <HeaderStyle Font-Bold="True" Width="75px" HorizontalAlign="Center"/>
          </asp:BoundColumn>
          <asp:BoundColumn DataField="MC" HeaderText="名稱">
            <HeaderStyle Font-Bold="True" Width="90px" HorizontalAlign="Center"/>
          </asp:BoundColumn>
          <asp:BoundColumn DataField="BT" HeaderText="標題">
            <HeaderStyle Font-Bold="True" HorizontalAlign="Center"/>
          </asp:BoundColumn>
        </Columns>
        <HeaderStyle BackColor="#7481BA" ForeColor="Yellow" />
      </asp:DataGrid>
    </div>
    <div>
      <input id="Btn_OK" type="button" onclick="OK()" value="確 定"/>
      <input id="Btn_Cancel" type="button" onclick="closeDiag()" value="取 消" />
    </div>
  </form>
</body>
</html>

示範中我順手把原本對不上名稱的 clickDiag 改成 closeDiag,否則按「取消」會直接丟出函式不存在的錯誤。另外要提醒的是,showModalDialog 只存在於舊版 IE,現代瀏覽器完全不支援,新專案請改用 window.open 或更好的做法是直接用頁內彈層。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved