选项卡式 UI 中的模式对话框(改进的示例)

发布于 2024-08-06 19:33:04 字数 2922 浏览 2 评论 0原文

(我已经加强了我原来的例子) 我试图从选项卡式 UI 中调用模式对话框,但我对所看到的行为感到困惑。第一次显示 UI 时,对话框的行为符合预期,我可以从字段中提取数据,一切都很棒。

tabtest2.html:

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Tabtest 2</title>

<link rel="stylesheet" type="text/css" href="js/css/smoothness/jquery-ui-1.7.2.custom.css" media="screen"/>  

<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.7.2.custom.min.js"></script>
<script type="text/javascript">

jQuery(document).ready(function()
{
  var tabs = $('#tabs').tabs({
    load: function(event, ui)
    {
      initializeUI();
    }
  });
});

function initializeUI()
{
  jQuery("#button1").click(function()
  {
     $(initializeUI.win).dialog("open");
  });

  $(initializeUI.win) = jQuery("#window1");

  //instantiate the dialog
  $(initializeUI.win).dialog({ height: 350,
                     width: 400,
                     modal: true,
                     position: 'center',
                     autoOpen:false,
                     title:'Create Agent',
                     overlay: { opacity: 0.5, background: 'black'},
                     buttons:
                     {
                        "Check Text 1": function()
                        {
                          var t1 = $("#text1");
                          alert("text 1 = " + t1.val());
                        },
                        "Close": function()
                        {
                            $(initializeUI.win).dialog("close");
                        }
                    }
               });
}
</script>

</head>   

<body>

<div id="tabs">
  <ul>
    <li><a href="tab1.html">Tab1</a></li>
    <li><a href="http://google.com">Google</a></li>
  </ul>
</div>

</body>
</html>

和 tab1.html

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Tab 1</title>

<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.7.2.custom.min.js"></script>

</head>   

<body>

<button id="button1" class="ui-button ui-state-default ui-corner-all">Button 1</button>

<div id="window1" style="display:none">
  <form>
    <fieldset>
      <label for="text1">Text 1</label>
      <input type="text" name="text1" id="text1" class="text ui-widget-content ui-corner-all" />
    </fieldset>
  </form>
</div>

</body>
</html>

这允许对话框(显然)处理重复的选项卡选择,但是当我尝试更改文本字段的内容并检查它时,我得到旧值(第一次调用的值) )!!就好像我创建了对话框及其字段的新副本,但原始文本字段在原始对话框窗口中看不见,返回的是旧结果。

显然,有一个我还没有掌握的处理这些对话框、div 和选项卡的范例。有人愿意指出我的错误吗?

(I have tightened up my original example)
I'm trying to invoke modal dialogs from within a tabbed UI, and I'm confused about the behavior I'm seeing. The first time I display the UI, my dialog behaves as expected, I can pull the data out of the fields, everything's wonderful.

tabtest2.html:

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Tabtest 2</title>

<link rel="stylesheet" type="text/css" href="js/css/smoothness/jquery-ui-1.7.2.custom.css" media="screen"/>  

<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.7.2.custom.min.js"></script>
<script type="text/javascript">

jQuery(document).ready(function()
{
  var tabs = $('#tabs').tabs({
    load: function(event, ui)
    {
      initializeUI();
    }
  });
});

function initializeUI()
{
  jQuery("#button1").click(function()
  {
     $(initializeUI.win).dialog("open");
  });

  $(initializeUI.win) = jQuery("#window1");

  //instantiate the dialog
  $(initializeUI.win).dialog({ height: 350,
                     width: 400,
                     modal: true,
                     position: 'center',
                     autoOpen:false,
                     title:'Create Agent',
                     overlay: { opacity: 0.5, background: 'black'},
                     buttons:
                     {
                        "Check Text 1": function()
                        {
                          var t1 = $("#text1");
                          alert("text 1 = " + t1.val());
                        },
                        "Close": function()
                        {
                            $(initializeUI.win).dialog("close");
                        }
                    }
               });
}
</script>

</head>   

<body>

<div id="tabs">
  <ul>
    <li><a href="tab1.html">Tab1</a></li>
    <li><a href="http://google.com">Google</a></li>
  </ul>
</div>

</body>
</html>

And tab1.html

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Tab 1</title>

<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.7.2.custom.min.js"></script>

</head>   

<body>

<button id="button1" class="ui-button ui-state-default ui-corner-all">Button 1</button>

<div id="window1" style="display:none">
  <form>
    <fieldset>
      <label for="text1">Text 1</label>
      <input type="text" name="text1" id="text1" class="text ui-widget-content ui-corner-all" />
    </fieldset>
  </form>
</div>

</body>
</html>

This allows the dialog to (apparently) work on repeated tab selections, but when I try to change the contents of the text field and examine it, I get the old value (the value from the first invocation)!! It's as if I have created a new copy of the dialog and it's fields, but the original text field is sitting there unseen in the original dialog window, returning it's old results.

Obviously, there's a paradigm for handling these dialogs, divs and tabs that I haven't grasped yet. Anyone care to point out my errors?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

哽咽笑 2024-08-13 19:33:04

在您的示例中,您两次使用相同的远程内容,更重要的是,在两个选项卡中使用相同的 ID。当第二个页面的内容加载到 DOM 中后,您将拥有两个具有相同 ID 的 div。由于 ID 在页面上应该是唯一的,因此“旧”值可能只是 javascript 在 DOM 中碰巧找到的第一个 div 的值。

您似乎还有两个 id 为“button1”的按钮;一个在模态 div 内,一个在模态 div 外部。这也可能会导致问题。

In your example you are using the same remote content twice and more importantly, using the same ID in both tabs. After the content of the second page is loaded into the DOM, you will have two divs with the same ID. Since an ID is supposed to be unique on a page, the "old" values may simple be the values of the first div that javascript happens to find in the DOM.

You also appear to have two buttons with the id "button1"; one inside the modal div and one outside. This may also cause problems.

紫罗兰の梦幻 2024-08-13 19:33:04

使用 FireBug,我发现每次调用 InitializeUI() 时都会创建一个新的“对话框”DIV 元素。所以删除旧的 DIV 似乎给了我想要的结果:


function initializeUI()
{
  jQuery("#button1").click(function()
  {
    initializeUI.win.dialog("open");
  });

  initializeUI.win = jQuery("#window1");

  // remove old 'dialog' DIV elements
  $('div[role="dialog"]').each(function() {
    $(this).remove();
  });

  //instantiate the dialog
  $(initializeUI.win).dialog({ height: 350,
                     width: 400,
                     modal: true,
                     position: 'center',
                     autoOpen:false,
                     title:'Create Agent',
                     overlay: { opacity: 0.5, background: 'black'},
                     buttons:
                     {
                        "Check Text 1": function()
                        {
                          var t1 = $("#text1");
                          alert("text 1 = " + t1.val());
                        },
                        "Close": function()
                        {
                          initializeUI.win.dialog("close");
                        }
                     }
                   });
}

Using FireBug, I see that I create a new 'dialog' DIV element everytime I call InitializeUI(). So deleting the old DIVs seems to give me the desired results:


function initializeUI()
{
  jQuery("#button1").click(function()
  {
    initializeUI.win.dialog("open");
  });

  initializeUI.win = jQuery("#window1");

  // remove old 'dialog' DIV elements
  $('div[role="dialog"]').each(function() {
    $(this).remove();
  });

  //instantiate the dialog
  $(initializeUI.win).dialog({ height: 350,
                     width: 400,
                     modal: true,
                     position: 'center',
                     autoOpen:false,
                     title:'Create Agent',
                     overlay: { opacity: 0.5, background: 'black'},
                     buttons:
                     {
                        "Check Text 1": function()
                        {
                          var t1 = $("#text1");
                          alert("text 1 = " + t1.val());
                        },
                        "Close": function()
                        {
                          initializeUI.win.dialog("close");
                        }
                     }
                   });
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文