[英]JQueryUI sortable only works once
我在页面标题中调用JQueryUI.js,然后使用AJAX加载内容。
jQuery拖放工作一次,然后退出工作。 如果我将每个AJAX内容加载到JqueryUI.js中,则它可以工作,但这非常慢。 这是在YII框架中
所以整个事情看起来像:
page.php文件:
$cs->registerScriptFile($baseUrl . '/js/jquery.ui.js');
$cs->registerScriptFile($baseUrl . '/js/product.js');
product.js:
// need to namespace this to get access to jquery.ui.js sortable function
(function($) {
SORTABLE = {
mySortable: function(inputId) {
$('#sortable-list-left').sortable();
$('#sortable-list-right').sortable();
}
};
}(jQuery));
$(document)
.ready(function() {
function loadRecord(id, sku) {
$.ajax({
data: "id=" + id + "&sku=" + sku + "&project_id=" + urlParam("project"),
url: "/product/ajaxGetRecord",
type: "POST",
success: function(response) {
// process JSON response
var obj = jQuery.parseJSON(response);
// update the html with the new info
$("#productForm")
.html(obj.form);
(function() {
SORTABLE.mySortable(urlParam("project"));
})();
}
});
});
_pagePartial.php:
<div id="productForm">
// dynamic content filled in here
</div>
这种命名间隔策略适用于我所有其他JS,但不适用于Jquery.ui.js ...它应用了sortable方法,并且我可以拖放一次,但随后不再起作用。
不理想的工作选项是:
_pagePartial.php:
// called every single ajax update ... ugh!
$cs->registerScriptFile($baseUrl . '/js/jquery.ui.js');
$('#sortable-list-left').sortable();
$('#sortable-list-right').sortable();
如果您不删除列表持有人(例如ul),并且其中包含动态li内容,则在ajax调用之后,无需重新绑定sortable()
。 否则,您必须用新的HTML内容替换列表,而必须这样做。 这是大卫·霍斯特 ( David Hoerster )的例子
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.