gpt4 book ai didi

javascript - 无法读取属性 "match"(nestedSortable)

转载 作者:行者123 更新时间:2023-11-30 17:03:10 26 4
gpt4 key购买 nike

我有一个树结构,我试图用 jQuery nestedSortable plugin 排序.

在这里你可以看到我的代码和一个工作示例:

$(document).ready(function(){
$('.sortable').nestedSortable({
handle: 'div',
items: 'li',
toleranceElement: '> div',
change: function(){
console.log('moved!');
console.log(this);
console.log($(this));
var arraied = $(this).nestedSortable('toArray', {startDepthCount: 0});
arraied = dump(arraied);
console.log(arraied);
}
});

});

function dump(arr,level) {
var dumped_text = "";
if(!level) level = 0;
//The padding given at the beginning of the line.
var level_padding = "";
for(var j=0;j<level+1;j++) level_padding += " ";

if(typeof(arr) == 'object') { //Array/Hashes/Objects
for(var item in arr) {
var value = arr[item];

if(typeof(value) == 'object') { //If it is an array,
dumped_text += level_padding + "'" + item + "' ...\n";
dumped_text += dump(value,level+1);
} else {
dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
}
}
} else { //Strings/Chars/Numbers etc.
dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
}
return dumped_text;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<link href="http://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css" rel="stylesheet"/>
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.min.js"></script>
<script src="http://mjsarfatti.com/sandbox/nestedSortable/jquery.mjs.nestedSortable.js"></script>
<ol class="sortable">
<li><div>List line 1</div></li>
<li>
<div>List line 2</div>
<ol>
<li><div>List line 3</div></li>
<li><div>List line 4</div></li>
</ol>
</li>
<li><div>List line 5</div></li>
</ol>

您可以在树周围移动节点,这样效果很好。不幸的是,我无法获得响应数组,因此无法更新我的数据库值。

如果您在移动这些微小的小节点时检查浏览器控制台,您会发现我不断收到错误消息:

"Uncaught TypeError: Cannot read property 'match' of undefined".

这个错误的原因可能是什么,同样重要的是,我怎样才能在树调整后得到一个数组?

最佳答案

在 jquery.mjs.nestedSortable v2.0) 中,替换第 769 行:

id = ($(item).attr(o.attribute || "id")).match(o.expression || (/(.+)[-=_](.+)/));

与:

id = ($(item).attr(o.attribute || "id") ||'').match(o.expression || (/(.+)[-=_](.+)/));

关于javascript - 无法读取属性 "match"(nestedSortable),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28499185/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com