带 jQuery ajax 加载的星级评定小部件

发布于 2024-12-23 10:05:15 字数 1035 浏览 0 评论 0原文

当页面首次加载到浏览器中时它工作正常

Without Problem

但是,当 DIV 容器包含此星级时,问题就会开始,再次使用 Ajax 加载进行加载。它被重置为原来的单选按钮:

with Problem

使此评级明星起作用的代码非常简单,如下所示:

星级评分小部件插件:

$("#stars-wrapper").stars({
  cancelShow: false,
  disabled: true
});

<span id="stars-wrapper">
                <input type="radio" id="rate1" name="newrate" value="1" title="Poor" />
                <input type="radio" id="rate2" name="newrate" value="2" title="Not that bad" />
                <input type="radio" id="rate3" name="newrate" value="3" title="Average" />
                <input type="radio" id="rate4" name="newrate" value="4" title="Good" />
                <input type="radio" id="rate5" name="newrate" value="5" title="Perfect" />
 </span> 

Ajax Load:

$('#content_pane').load( href );

有人知道如何解决这个问题吗?它似乎不能很好地处理 Ajax 负载。

It works fine when the page first loads into browser

Without problem

However, problem starts when the the DIV container contains this star rating, was loaded again with Ajax load. It was reset to its original radio button:

with problem

The code to made this rating star works was just as simple as following:

Star Rating Widget Plugin:

$("#stars-wrapper").stars({
  cancelShow: false,
  disabled: true
});

<span id="stars-wrapper">
                <input type="radio" id="rate1" name="newrate" value="1" title="Poor" />
                <input type="radio" id="rate2" name="newrate" value="2" title="Not that bad" />
                <input type="radio" id="rate3" name="newrate" value="3" title="Average" />
                <input type="radio" id="rate4" name="newrate" value="4" title="Good" />
                <input type="radio" id="rate5" name="newrate" value="5" title="Perfect" />
 </span> 

Ajax Load:

$('#content_pane').load( href );

Does anyone have idea how to tackle this problem? It seems to be not working well with Ajax load.

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

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

发布评论

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

评论(1

太傻旳人生 2024-12-30 10:05:15

您遇到的问题是您当前仅绑定到页面上已存在的元素。当您使用 AJAX 请求将新数据加载到页面中时,您的星星小部件不是活动的,因此不会绑定到这些新元素。

您需要在 AJAX 加载中重新绑定到这些元素,如下所示:

$('#content_pane').load( href, function() {
  $("#stars-wrapper").stars({
    cancelShow: false,
    disabled: true
  });
});

也许您的小部件包含 live API,但 live 已在 jQuery 中被弃用。

The problem you're encountering is that you're currently only binding to elements that already exist on the page. When you load new data into the page using an AJAX request, your stars widget is not live and is thus not bound to these new elements.

You would need to re-bind to these elements within your AJAX load like so:

$('#content_pane').load( href, function() {
  $("#stars-wrapper").stars({
    cancelShow: false,
    disabled: true
  });
});

Perhaps your widget includes a live API, but live has since been deprecated in jQuery.

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