jQuery - 通过 .load() 多次加载模式对话框表单并选择列表中断

发布于 2024-09-12 03:11:56 字数 1839 浏览 1 评论 0原文

我正在使用 jquery.load() (1.4.2) 加载内容,其中包括一个我将其转换为模型对话框的表单。当我打开对话框时,我动态填充表单选择列表。

它在我第一次加载内容时有效,但在后续加载时它会中断,并且只会返回上次加载中最后选定的项目。 (我还认为它不会在后续加载时重新加载选择列表,即使对话框打开功能上的警报触发。)

我将内容加载为:

<script>
...
 $("#content").load("test.html")
..
</.script>
...
<div id="content"></div>

和 test.html 文件:

<script type="text/javascript">
jQuery(document).ready(function(){
  $("#testForm").dialog({
    autoOpen: false,
    height: 400,
    width: 700,
    modal: true,
    buttons: {
      'Alert': function() {
        alert($("#testSelectList").val());
      },
      'Close': function() {
        $(this).dialog('close');
      }
    },
    close: function() {
    },
    open: function() {
      alert("Open");
      $("#testSelectList").html("<option value=\"one\">one</option><br/>"+
         "<option value=\"two\">two</option><br/>"+
         "<option value=\"three\">three</option><br/>"+
         "<option value=\"four\">four</option><br/>");
    }
    });
  $("#testButton").button().live('click', function() {$("#testForm").dialog('open');});
});
</script>

<div id="testForm" title="Test">
  <form>
  <fieldset>
    <label for="testSelectList">Select List</label><br/>
    <select id="testSelectList" class="ui-state-default ui-corner-all">
    <option value="">-none-</option></select><br/>
  </fieldset>
  </form>
</div>

<div id="test">
  <h1>Form Select List Test</h1>
  <button id="testButton">Open Test Form</button>
</div>

在本例中,在第二个 .load 上(),如果我点击警报按钮,它不会返回选定的列表项 - 它只会返回第一个项目。

我尝试将 .live() 放在按钮周围,单击打开对话框...但这不会改变任何内容。

I am loading content with jquery.load() (1.4.2), which includes a form that I turn into a model dialog. When I open the dialog, I dynamically populate a form select list.

It works the first time I load the content, but on subsequent loads it breaks and will only return the last selected item from the previous load. (I also think it doesn't reload the select list on subsequent loads, even though the alert on the dialog open function fires.)

I load the content as:

<script>
...
 $("#content").load("test.html")
..
</.script>
...
<div id="content"></div>

And the test.html file:

<script type="text/javascript">
jQuery(document).ready(function(){
  $("#testForm").dialog({
    autoOpen: false,
    height: 400,
    width: 700,
    modal: true,
    buttons: {
      'Alert': function() {
        alert($("#testSelectList").val());
      },
      'Close': function() {
        $(this).dialog('close');
      }
    },
    close: function() {
    },
    open: function() {
      alert("Open");
      $("#testSelectList").html("<option value=\"one\">one</option><br/>"+
         "<option value=\"two\">two</option><br/>"+
         "<option value=\"three\">three</option><br/>"+
         "<option value=\"four\">four</option><br/>");
    }
    });
  $("#testButton").button().live('click', function() {$("#testForm").dialog('open');});
});
</script>

<div id="testForm" title="Test">
  <form>
  <fieldset>
    <label for="testSelectList">Select List</label><br/>
    <select id="testSelectList" class="ui-state-default ui-corner-all">
    <option value="">-none-</option></select><br/>
  </fieldset>
  </form>
</div>

<div id="test">
  <h1>Form Select List Test</h1>
  <button id="testButton">Open Test Form</button>
</div>

In this example, on a second .load(), if I hit the alert button, it won't return the selected list item - it will only return the first item.

I've tried putting .live() around the button click opening the dialog... but that doesn't change anything.

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

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

发布评论

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

评论(2

梦情居士 2024-09-19 03:11:56

当您创建一个对话框时,它会将其移动到 的末尾(更重要的是,在 #content 之外,因此当 #content< /code> 已重新加载)。那么发生的事情是您的 .load() 获取该内容,但是该对话框仍然存在,并且您在第一次加载后将使用重复 ID 提取内容。

确保首先销毁该对话框/原始内容以避免重复问题,如下所示:

$("#testForm, #test").remove();
$("#content").load("test.html");

请注意 。 remove() 清理原本会留下的事件处理程序。

这样,就不需要 .live() 处理程序,因为您现在一次可以正确地拥有一个按钮和一个对话框,而不是多个共享相同的 ID。

When you create a dialog it's moving it to the end of the <body> (more important, outside #content, so it escapes death when #content is re-loaded). So what happens is your .load() gets that content, but the dialog is left around, and you're pulling content with duplicate IDs after the first load.

Make sure to destroy that dialog/original content first to avoid the duplicate issue, like this:

$("#testForm, #test").remove();
$("#content").load("test.html");

Note that .remove() cleans up the event handlers that would otherwise be left around as well.

With this, there's no need for the .live() handler either, since you'll now correctly have one button and one dialog at a time, instead of multiple sharing the same IDs.

镜花水月 2024-09-19 03:11:56

我会在你的加载函数上放置一个回调并在那里运行你的javascript

$("#content").load("test.html", function(){
//Javascript code
});

I would put a callback on your load function and run your javascript there

$("#content").load("test.html", function(){
//Javascript code
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文