假设你所有的选择是正确的,那么你应该能够做到这一点:
$.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...