jquery - jquery sortable 和 guids 的问题

标签 jquery html jquery-ui-sortable

我有

<ul id="test-list" class="ui-sortable" >
   <li id="876e45a4-6a27-4f7b-95ca-9e2500f11801">text1</li>
   <li id="4df36ffb-561a-4eaa-9b1c-9e2500f16d6b">text2</li>
   <li id="cc4a57d5-1567-403c-888f-9e2500f1c171">text3</li>
</ul>

但是 $('#test-list').sortable('serialize'); 执行这样的字符串: 876e45a4-6a27-4f7b-95ca[]=9e2500f11801&79fbfdf7-e9b1-4e96-8e42[]=9e2400d2abb7&4df36ffb-561a-4eaa-9b1c[]

删除 id 的最后 12 个字符

有什么想法吗?

最佳答案

两件事:

Sortable.serialize 的 ID

您确定 Sortableserialize 会执行您期望的操作吗?根据the docs ,它期望每个可排序元素的 ID 的格式为 setname_number。 ID 应包含下划线,但会回退到使用 =-;来自 the docs :

It works by default by looking at the id of each item in the format 'setname_number', and it spits out a hash like "setname[]=number&setname[]=number".
...They must be in the form: "set_number" For example, a 3 element list with id attributes foo_1, foo_5, foo_2 will serialize to foo[]=1&foo[]=5&foo[]=2. You can use an underscore, equal sign or hyphen to separate the set and number. For example foo=1 or foo-1 or foo_1 all serialize to foo[]=1.

(我的重点。)所以没有看到下划线 (_),显然它正在退回到使用它看到的最后一个连字符 (-)。

id 用于 CSS 的格式

另外,用于 CSS 的 id不能以数字开头;来自 the spec :

In CSS, identifiers (including element names, classes, and IDs in selectors) can contain only the characters [a-zA-Z0-9] and ISO 10646 characters U+00A1 and higher, plus the hyphen (-) and the underscore (_); they cannot start with a digit, or a hyphen followed by a digit.

(这些限制不适用于 HTML 中的 id,它不能包含空格并且必须至少包含一个字符,但 CSS 限制更多。)

由于您在 jQuery 中使用 CSS 选择器,因此最好确保 id 属性是有效的 CSS。 (也许您可以将它们预处理为“G”,后接 GUID,破折号转换为 x,诸如此类,因此 ID 为 G876e45a4x6a27x4f7bx95cax9e2500f11801 等。 ) 然后你会想要按照上面的方式给他们一个下划线和后缀。

关于jquery - jquery sortable 和 guids 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4238511/

相关文章:

javascript - 在 jQuery AJAX 成功中从 MySql 获取特定响应

javascript - 通过单击动态更改 li 选择

javascript - 单击其他内容时如何关闭某些内容?

jquery一张一张显示图片

javascript - 如何获得使用 css 转换的元素的最终高度?

javascript - JQuery 在调整图像 Canvas 大小后仅调整图像而不是 Canvas

php - 尝试使用 $_SESSION ['bandname' 将乐队名称附加到 mp3 上传];

javascript - 使用 JQuery 对列表项进行排序

jquery - 使用 jQuery 对表行的 id 重新编号

javascript - 阻止除特定元素之外的所有元素