我有
<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
您确定 Sortable
的 serialize
会执行您期望的操作吗?根据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 attributesfoo_1
,foo_5
,foo_2
will serialize tofoo[]=1&foo[]=5&foo[]=2
. You can use an underscore, equal sign or hyphen to separate the set and number. For examplefoo=1
orfoo-1
orfoo_1
all serialize tofoo[]=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/