ajax请求参数格式化,格式化为Ajax POST请求参数的Rails控制器 - jQuery的UI排序列表(Format...

假设你所有的选择是正确的,那么你应该能够做到这一点:

$.ajax

url: "sort"

type: "POST"

data: { Activity: JSON.stringify(neworder) }

您还可以简化您的代码的其余部分:

update: (event, ui) ->

neworder = [ ]

$('[id*="day"] > li').each ->

# 'this' should be the DOM element so just read off the 'id' attribute,

# you don't need to waste time building a jQuery object just to get an

# 'id' attribute.

column = @id

index = ui.item.index() + 1

# String interpolation to skip the string addition noise. You already

# have a jQuery object here so there's nothing wrong with using

# attr('id') here.

id = $("##{column} li:nth-child(#{index})").attr('id')

# You don't need 'passObject' here, just use an object literal and push

# it onto neworder all in one go.

neworder.push(

id: id

column: column

index: index

)

$.ajax

url: "sort"

type: "POST"

data: { Activity: JSON.stringify(neworder) }

甚至更紧凑(如果你喜欢那种事):

update: (event, ui) ->

neworder = [ ]

$('[id*="day"] > li').each ->

index = ui.item.index() + 1

neworder.push(

id: $("##{column} li:nth-child(#{index})").attr('id')

column: @id

index: index

)

$.ajax

url: "sort"

type: "POST"

data: { Activity: JSON.stringify(neworder) }

然后,你可以JSON.parse(params[:Activity])把它解压到您的控制器。

一点在意见讨论后,我觉得你的index值是错误的。 这样做:

index = ui.item.index() + 1

会给你同样的index为每个

你可能想要更多的东西是这样的:

$lis = $('[id*="day"] > li')

$lis.each ->

index = $lis.index(@)

#...

这应该给你目前的指数

@集合内) 是你们等会遍历。 另外,您也可以使用该参数each传递到迭代器功能:

$('[id*="day"] > li').each (index, el) ->

# Just use `index` as-is in here...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值