使用 Rails 进行 jQuery 序列化不适用于自定义 ID
我很确定这是某种 Javascript 错误,而不是 Rails 错误,在对我的 id 进行了大量的 put() 试验之后...
我想做的是使用 jQuery 的序列化函数序列化一堆 li。我想基于一组自定义的 id 来执行此操作...在我的例子中,podli-1、podli-2、podli-3 等。我尝试使用序列化“表达式”选项来执行此操作。
当我尝试使用警报对其进行调试时(如下所示),我得到的结果是: id=undefined&id=undefined&id=undefined&id=undefined&id=undefined&id=undefined&id=未定义
——这实际上是正确的 li 数量,但由于某种原因,所有 id 都未定义。正如我所说,这一切都在 Rails 中运行,但存在一些 JS 错误。
这是 Rails 代码。
<% @pods.each do |podli| %>
<% @podli = podli %>
<% @iden = 'podli-' << @podli.id.to_s %>
<li id="<%= @iden %>" class="ui-state-default">
....
</li>
<% end %>
这是 JavaScript
$(document).ready(function(){
transitions();
//$( "#sortable" ).sortable({containment: 'parent'});
$( "#sortable" ).sortable({ items: '.ui-state-default', containment: 'parent'})//for moving the pods about
$( "#sortable" ).disableSelection();
//$("#newspod" ).val("Enable Sort").closest("#newspod").removeClass("sortable")
$('#sortable').live('sortstop', function(event) {
var u = $(this).sortable('serialize', { key: 'id', expression: /podli-\d/ })// + '&index=' + jQuery(this).attr("id").substring(19);
alert(u);
$.ajax({
type: 'POST',
data: { display_order: u },//$('#receiving-list').sortable('serialize') + '&index=' + jQuery(this).attr("id").substring(19),
url: "<%= sort_dashboards_path %>"
})
});
});
I'm pretty sure this is some sort of Javascript error and not a Rails error after doing numerous puts() trials on my id's...
What I'm trying to do is serialize a bunch of li's using jQuery's serialize function. I want to do it based on a custom set of id's... In my case, podli-1, podli-2, podli-3, etc. I try to do this by using the option of serialize "expression".
What I get back from this when I try to debug it with alerts (as you can see below) is this: id=undefined&id=undefined&id=undefined&id=undefined&id=undefined&id=undefined&id=undefined
-- which is actually the right number of li's but for some reason all the id's are undefined. As I've said, it's all working within Rails, but there's some JS error.
Here's the Rails code.
<% @pods.each do |podli| %>
<% @podli = podli %>
<% @iden = 'podli-' << @podli.id.to_s %>
<li id="<%= @iden %>" class="ui-state-default">
....
</li>
<% end %>
And here's the javascript
$(document).ready(function(){
transitions();
//$( "#sortable" ).sortable({containment: 'parent'});
$( "#sortable" ).sortable({ items: '.ui-state-default', containment: 'parent'})//for moving the pods about
$( "#sortable" ).disableSelection();
//$("#newspod" ).val("Enable Sort").closest("#newspod").removeClass("sortable")
$('#sortable').live('sortstop', function(event) {
var u = $(this).sortable('serialize', { key: 'id', expression: /podli-\d/ })// + '&index=' + jQuery(this).attr("id").substring(19);
alert(u);
$.ajax({
type: 'POST',
data: { display_order: u },//$('#receiving-list').sortable('serialize') + '&index=' + jQuery(this).attr("id").substring(19),
url: "<%= sort_dashboards_path %>"
})
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您调用“序列化”方法(通过“可排序”插件)时,您的“表达式”正则表达式必须包含一个捕获组,无论值应该是什么:
通过包装“\d”(请注意,我无法帮助我和我自己添加了“+”,如果您在括号中包含超过 10) 个,则该插件将从您的正则表达式中获得它所期望的内容。
该方法的 jQuery UI 文档非常糟糕。
When you call the "serialize" method (via the "sortable" plugin), your "expression" regex has to include a capturing group around whatever the value should be:
By wrapping the "\d" (note that I couldn't help myself and I added "+" to that, in case you ever have more than 10) in parentheses, the plugin will get what it expects from your regex.
The jQuery UI documentation for that method is awful.