【问题标题】:Cannot display more than one popup simulteneously不能同时显示多个弹出窗口
【发布时间】:2016-03-14 10:08:39
【问题描述】:

我想同时显示 4 个弹出窗口,在单击相应的图像按钮后显示 4 个不同的 Flash 幻灯片。

我使用JavaScript 制作了弹出窗口,但无法完成图像按钮的其余部分。当我单击按钮时,随机播放幻灯片。

请参考以下代码:-

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
<link href="ddmenu/ddmenu.css" rel="stylesheet" type="text/css" />
<link rel="shortcut icon" href="images/favicon.ico"/>
<script src="ddmenu/ddmenu.js" type="text/javascript"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.9/jquery-ui.js" type="text/javascript"></script>
    <link href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.9/themes/start/jquery-ui.css" rel="stylesheet" type="text/css" />

    <script type="text/javascript">
        $("[id*=a]").live("click", function () {
            $("#dialog26").dialog({
                title: "DMD Officers",
                height: 700,
                width: 1000,
                buttons: {
                    Close: function () {
                        $(this).dialog('close');
                    }
                }
            });
            return false;
        });
    </script>

    <script type="text/javascript">
        $("[id*=b]").live("click", function () {
            $("#dialog27").dialog({
                title: "Outsourced Photographs",
                height: 700,
                width: 1000,
                buttons: {
                    Close: function () {
                        $(this).dialog('close');
                    }
                }
            });
            return false;
        });
    </script>

    <script type="text/javascript">
        $("[id*=c]").live("click", function () {
            $("#dialog28").dialog({
                title: "DMD Dog Squad",
                height: 700,
                width: 1000,
                buttons: {
                    Close: function () {
                        $(this).dialog('close');
                    }
                }
            });
            return false;
        });
    </script>

    <script type="text/javascript">
        $("[id*=d]").live("click", function () {
            $("#dialog29").dialog({
                title: "Snake awareness campaign",
                height: 700,
                width: 1000,
                buttons: {
                    Close: function () {
                        $(this).dialog('close');
                    }
                }
            });
            return false;
        });
    </script>

</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table>
<tr>
            <td colspan="3" class="auto-style9">
                <nav id="ddmenu">
    <ul>
    <li><a href="Home.aspx" target="_blank">Home</a></li>
    <li><a href="http://www.ril.com/OurBusinesses/Global-Corporate-Security/Security-Services.aspx" target="_blank">About Us</a></li>
</ul>

</nav>
</td>  
</tr>

<tr>
                    <td style="border-style: solid; border-color: #996600;">
                        <asp:imagebutton id="a" runat="server" height="200px" imageurl="~/Images/officer.jpg" width="200px" />
                        <div id="dialog26" style="display: none;">
                            <embed src="Videos/DMD Officers final.swf" height="1000px" width="1000px" />
                        </div>
                    </td>
                    <td style="border-style: solid; border-color: #996600;">
                        <asp:imagebutton id="b" runat="server" height="200px" imageurl="~/Images/outphoto.jpg" width="200px" />
                        <div id="dialog27" style="display: none;">
                            <embed src="Videos/Outsourced photographs.swf" height="1000px" width="1000px" />
                        </div>
                    </td>
                    <td style="border-style: solid; border-color: #996600; text-align: center;">
                        <asp:imagebutton id="c" runat="server" height="200px" imageurl="~/Images/Dog.jpg" width="200px" />
                        <div id="dialog28" style="display: none;">
                            <embed src="Videos/DMD Dog Squad 1.swf" height="1000px" width="1000px" />
                        </div>
                    </td>
                    <td style="border-style: solid; border-color: #996600; text-align: center;">
                        <asp:imagebutton id="d" runat="server" height="200px" imageurl="~/Images/SankeAwa.jpg" width="200px" />
                        <div id="dialog29" style="display: none;">
                            <embed src="Videos/Snake awareness campaign.swf" height="1000px" width="1000px" />
                        </div>
                    </td>
                </tr>
            </table>
        </div>
    </form>
</body>
</html>

【问题讨论】:

  • 可能会让你的问题更清楚,关于你的程序的哪一部分失败了,哪一部分你需要帮助。您也可以放置一个 Jsfiddle 来演示问题
  • 不要使用LIVE 来绑定事件。请改用on

标签: javascript jquery html css asp.net


【解决方案1】:

这里的代码$("[id*=btnPopup]") 表示捕获所有包含btnPopup 的按钮!,因为其余按钮包含该名称(btnPopup2,btnPopup3,btnPopup4) 您已经使用相同的(第一个)代码捕获了所有内容,并且您只按下第一个代码。

所以,如果您想按原样使用此代码并使其正常工作,请将第一个按钮重命名为 btnPopup0

参考: https://api.jquery.com/attribute-contains-selector/

但是我看到了更多的错误......

在每次调用都存在的这一行$("#dialog").dialog({ 中,仍然调用页面的同一部分以使其成为对话框,但我看到其他部分的名称如 dialog1dialog2 等等...所以这也是你必须解决的问题。

【讨论】:

    【解决方案2】:

    您的代码中有很多错误! .让我一一指出。

    1. $("[id*=a]").live("click" : 类似的代码存在于其他 3 个部分。 请勿使用 Live 绑定事件。它已被弃用,并且有充分的理由被弃用。取自文档

    从 jQuery 1.7 开始,不推荐使用 .live() 方法。使用 .on() 附加事件处理程序。旧版本 jQuery 的用户应该使用 .delegate() 而不是 .live()。

    在您的场景中,[id*=a] 也不是必需的。只需id=a 就足够了。

    live() jquery API Document

    2. $("#dialog").dialog({ :您在其他 3 个部分中也有相同的代码。您正在将事件绑定到相同的 id。这是BIG NO。不要使用相同的 Id Id 在整个 DOM 中必须是唯一的。你可以利用类。只需将相同的类名放在所有 div 中,然后使用类名绑定事件。 - 当您使用ids 绑定事件时,Jquery 将在 DOM 中找到具有该 id 的 first 元素(从顶部读取)并将事件绑定到仅该元素。



    您多次重复相同的脚本逻辑,make it Generic。所以清理你的 HTML 和 SCripts,下面是你真正需要的。

    HTML

        <tr>
          <td style="border-style: solid; border-color: #996600;">
            <asp:imagebutton id="a" class="imageDialog" runat="server" height="200px" imageurl="~/Images/officer.jpg" width="200px" />
            <div class="dialog" data-dialog-title="DMD Officers" style="display: none;">
              <embed src="Videos/DMD Officers final.swf" height="1000px" width="1000px" />
            </div>
          </td>
          <td style="border-style: solid; border-color: #996600;">
            <asp:imagebutton id="b" class="imageDialog" runat="server" height="200px" imageurl="~/Images/outphoto.jpg" width="200px" />
            <div class="dialog" data-dialog-title="Outsourced photographs" style="display: none;">
              <embed src="Videos/Outsourced photographs.swf" height="1000px" width="1000px" />
            </div>
          </td>
          <td style="border-style: solid; border-color: #996600; text-align: center;">
            <asp:imagebutton id="c"  class="imageDialog" runat="server" height="200px" imageurl="~/Images/Dog.jpg" width="200px" />
            <div class="dialog" data-dialog-title="DMD Dog Squad" style="display: none;">
              <embed src="Videos/DMD Dog Squad 1.swf" height="1000px" width="1000px" />
            </div>
          </td>
          <td style="border-style: solid; border-color: #996600; text-align: center;">
            <asp:imagebutton id="d"  class="imageDialog" runat="server" height="200px" imageurl="~/Images/SankeAwa.jpg" width="200px" />
            <div class="dialog" data-dialog-title="Snake awareness campaign" style="display: none;">
              <embed src="Videos/Snake awareness campaign.swf" height="1000px" width="1000px" />
            </div>
          </td>
        </tr>
    

    注意: I added a class to all the imageButtonAnd then changed the div id to class。还添加了data-dialog-title 属性来保存对话框的标题。 More About data Attribute你会在下面看到它的用法。

    脚本

    <script type="text/javascript">
    $(function(){
      $.each('.dialog',function(){ //loop all the div's and apply plugin for all of them
        $(this).dialog({
          title: $(this).data('dialog-title'), //extract the title here.
          height: 700,
          width: 1000,
          buttons: {
            Close: function() {
              $(this).dialog('close');
            }
          }
        });
     });
    
    
      $(".imageDialog").on("click", function() {
         $(this).closest('td').find(".dialog").dialog( "open" ); //this is how you open the dialog, taken from the plugin site.
      });
    });
    </script>
    
    • 在文档准备就绪时,将对话框插件应用到所有具有类 dialog 的 div
    • 使用类选择器.imageDialog将事件绑定到所有imageButton
    • 向上追溯至父 td,然后向下追溯以找到类名为 'dialog' 的 div 并打开它。

    【讨论】:

    • 我已按照您的代码进行操作,但是当我单击图像按钮时,弹出窗口没有出现,当我将 on 更改为 live 时,它可以正常工作,但是当我第二次单击同一个图像按钮时弹出窗口没有出现。
    • @JayDesai 如果您说 live 正在工作而不是在进行,这意味着您正在加载 HTML 之前的脚本。您没有在准备好的文档上加载脚本。我已经更新了我的答案以在准备好的文档上运行脚本,使用它并让我知道它是否解决了问题。还告诉我你把脚本块放在哪里了??在 HTML 之后还是之前?如果您的 HTML 是使用 jquery 动态构建的,请告诉我。
    • 现在弹出窗口来了,但是当我第二次点击相同的图像按钮时,弹出窗口没有出现。我已将脚本放在头上,并通过放入单独的脚本文件进行检查,但问题仍然存在。
    • 问题很明显,您不能将插件两次应用于同一元素。 $(this).closest('td').find(".dialog").dialog({ 为每个按钮点击应用插件到 div。如果您再次单击该按钮,这将不起作用。因为它正在尝试将插件应用于已应用的元素。我还以为您在单击按钮时试图应用插件。我不知道你只是想要每个按钮点击的弹出窗口。
    • @JayDesai 你能给我你正在使用的插件文档的链接吗??
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-19
    • 2021-06-03
    • 1970-01-01
    • 1970-01-01
    • 2020-10-12
    • 1970-01-01
    相关资源
    最近更新 更多