MVC 部分视图 jQuery 日期选择器删除以前的日期选择器

发布于 2024-09-09 05:14:44 字数 996 浏览 7 评论 0原文

我有一个页面,我试图将子对象添加到父对象。

在页面上,我有一个链接“添加子项”,它调用 ajax get,返回带有子项文本框的部分视图。在成功中,我正在执行一个函数将结果添加到 div 的 innerHTML 中。

在部分视图上,我有一个出生日期文本框,名为“Birthdate{id}”,其中 {id} 是基于已添加的数字的数字。

在成功方法中,我正在

$("#BirthDate" + childAdded).datepicker();

为最新的生日日期选择器做一个“这非常有效”。如果我添加 3 个孩子,则第 3 个孩子 div 的出生日期将加载一个日期选择器,但第 1 个和第 2 个孩子将丢失日期选择器。添加第四个日期将会从第三个日期中删除日期选择器。

代码:

function addChild() {
  $.ajax({
    type: "GET",
    url: "/Home/AddChild?childNumber=" + $("#CurrentNumberOfChildren").val(),
    data: "{}",
    contentType: "text/html",
    dataType: "html",
    success: function (results) {
      var div = document.getElementById('ChildDiv');
      div.innerHTML += results;
      $("#CurrentNumberOfChildren").val(parseInt($("#CurrentNumberOfChildren").val()) + 1);
      var numberOfChildren = $("#CurrentNumberOfChildren").val();
      $("#BirthDate" + numberOfChildren).datepicker();
    }
  });
}

有人遇到过这种情况吗?

I have a page where I am trying to add Children to a Parent object.

On the page, I have a link, "Add Child", that calls an ajax get, returning a partial view with textboxes for the Child. In the sucess, i am doing a function to add the results to the innerHTML of a div.

On the partial view, I have a textbox for the Birthdate, named "Birthdate{id}", where the {id} is a number based on how many have been added.

In the success method, I am then doing a

$("#BirthDate" + childAdded).datepicker();

This works great, for the latest Birthdate date picker. If I add 3 Children, the 3rd Child div's BirthDate will have a datepicker on load, but the 1st and 2nd will lose the datepicker. Adding a 4th will then remove the datepicker from the 3rd.

Code:

function addChild() {
  $.ajax({
    type: "GET",
    url: "/Home/AddChild?childNumber=" + $("#CurrentNumberOfChildren").val(),
    data: "{}",
    contentType: "text/html",
    dataType: "html",
    success: function (results) {
      var div = document.getElementById('ChildDiv');
      div.innerHTML += results;
      $("#CurrentNumberOfChildren").val(parseInt($("#CurrentNumberOfChildren").val()) + 1);
      var numberOfChildren = $("#CurrentNumberOfChildren").val();
      $("#BirthDate" + numberOfChildren).datepicker();
    }
  });
}

Anyone ever run into this?

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

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

发布评论

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

评论(4

复古式 2024-09-16 05:14:44

这行 -- div.innerHTML += results; -- 每次添加新的子部分时都会重写 div 的整个内容。这意味着您将丢失 div 中先前元素上的事件挂钩等,即使重写的元素使用相同的 id 等。我怀疑这就是破坏你的日期选择器的原因。

要修复此问题,您需要将新内容附加到 div 中已有的内容,而不是每次都重写整个内容。尝试替换这两行:

var div = document.getElementById('ChildDiv');
div.innerHTML += results;

用这一行:

$('#ChildDiv').append(results);

This line -- div.innerHTML += results; -- is rewriting the entire content of the div each time you add a new child section. This means that you're losing the event hooks etc on the elements that were previously in the div, even though the rewritten elements use the same ids etc. I suspect that this is what's breaking your datepickers.

To fix it you'll need to append the new content to what's already in the div, rather than rewriting the entire content every time. Try replacing these two lines:

var div = document.getElementById('ChildDiv');
div.innerHTML += results;

With this one:

$('#ChildDiv').append(results);
贱贱哒 2024-09-16 05:14:44

据我所知,您可能有多个元素共享相同的 id。 jQuery 需要一个有效的 dom 才能正常工作。您可以在 w3c 验证您的标记。

您能否将部分视图的响应添加到您的问题中,以及 jquery 和 jquery 的版本?用户界面?

From what I can see you have probably got multiple elements sharing the same id. jQuery needs a valid dom to work correctly. You can validate your markup at w3c.

Can you add the response of the partial view to your question, also what version of jquery & ui?

薯片软お妹 2024-09-16 05:14:44

我假设您的部分视图返回所有文本框,包括以前的文本框和新文本框,因为您正在用结果替换 div 的 HTML。在这种情况下,先前的元素(您添加了日期选择器的元素)已从页面中删除。如果是这种情况,那么您需要将日期选择器重新添加到所有 BirthDate 元素中。

I'm assuming that your partial view returns all of the textboxes, the previous ones plus the new ones since you are replacing the HTML of the div with the result. In that case, the previous elements (to which you added the datepickers) have been removed from the page. If that's the case then you'll need to re-add the datepickers to all of your BirthDate elements.

醉生梦死 2024-09-16 05:14:44

.live() – jQuery API

jQuey 怎么样。居住?

尽管改变选择器是必要的。

.live() – jQuery API

How about jQuey.live?

Though the change of selector is necessary.

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