JS/Jquery 填充数组然后通过 AJAX 发送
我正在使用 JQuery UI 的 sortable 通过将每个元素拖动到所需的顺序来对列表 #sortable
进行排序。
我将商品的 id
存储在 li
id
属性中(我知道这是无效的),以及 order<
rel
属性中元素的 /code>。
例如:
<li id="23" rel="1">First</li>
<li id="20" rel="2">Second</li>
<li id="24" rel="3">Third</li>
当我拖放 li
元素时,我的代码成功更新了 rel
- 这很好。
现在,当我单击提交按钮时,我想通过 AJAX 将一组数据发送到我的脚本,该脚本将执行更新查询。理想情况下,id
将是key
,rel
值将是“value”。
array(
23 => 1
20 => 2
24 => 3
)
到目前为止,这是我的代码:
$('#submit').click(function(e) {
e.preventDefault();
//array
var the_data = [];
$('#sortable li').each(function() {
the_data[$(this).attr('id')] = $(this).attr('rel');
});
console.log(the_data);
});
我在 FireBug 中多次收到 undefined
,但是如果我展开它,所有值都在那里,有人可以解释一下出了什么问题吗?至少对我来说这是有道理的。
将数据数组发送到 AJAX 的最佳方式是什么?我已经阅读过有关 Jquery 的 .param()
和 serialize
这方面有什么最佳实践吗?
我的阿贾克斯:
myData=Jquery.param(the_data); //serialize the array?
$.ajax({
type: "POST",
url: "<?php echo base_url(); ?>admin/update_order/",
data: myData,
success: function(msg) {
alert('Updated');
},
error: function(msg) {
alert(msg);
}
});
谢谢。
I am using JQuery UI's sortable to sort a list #sortable
by dragging each element in to the desired order.
I am storing the id
of the item in the li
id
attribute (I am aware this is invalid), and the order
of the element in the rel
attribute.
So for example:
<li id="23" rel="1">First</li>
<li id="20" rel="2">Second</li>
<li id="24" rel="3">Third</li>
My code successfully updates the rel
when I drag and drop a li
element - this is fine.
Now, when I click the submit button, I would like to send an array of data via AJAX to my script, which will perform the update queries. Ideally, the id
will be the key
and the rel
value will be the `value.
array(
23 => 1
20 => 2
24 => 3
)
Here is my code thus far:
$('#submit').click(function(e) {
e.preventDefault();
//array
var the_data = [];
$('#sortable li').each(function() {
the_data[$(this).attr('id')] = $(this).attr('rel');
});
console.log(the_data);
});
I'm getting undefined
lots of times in FireBug, but if I expand it, all the values are there, could someone please explain what is wrong? It makes sense to me, at least.
What is the best way to send an array of data to AJAX? I have read about Jquery's .param()
and also serialize
Are there any best practices for this?
My ajax:
myData=Jquery.param(the_data); //serialize the array?
$.ajax({
type: "POST",
url: "<?php echo base_url(); ?>admin/update_order/",
data: myData,
success: function(msg) {
alert('Updated');
},
error: function(msg) {
alert(msg);
}
});
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不会自己写代码。只需使用 jQuery UI Sortable 中继承的功能即可。
绑定一个停止事件函数并使用 .sortable('serialize') 以新顺序获取列表,其格式可以轻松通过 Ajax 传递。
http://jqueryui.com/demos/sortable/#method-serialize
I wouldn't write the code myself. Just use the functionality inherit in jQuery UI Sortable.
Tie in a stop event function and use .sortable('serialize') to get the list in the new order in a format easily passed through Ajax.
http://jqueryui.com/demos/sortable/#method-serialize
如果您查看 jQuery UI 的可排序文档,它会显示 2 种方法来获取 ID 数组以满足您的目的。我会特别关注 serialize 方法,因为它更接近您想要的。特别是如果您可以放弃 rel 属性并直接转向带下划线的 id。
If you look at the docs for jQuery UI's sortable, it shows 2 methods to get an array of the ID's back for your purpose. I'd look specifically at the serialize method as it gets closer to what you want. Especially if you can forgo the rel attribute and just move to an underscored id.
请正确遵循此链接,以对 jquery 可排序的 ajax 内容进行操作并绑定到更新函数,请参阅此 fiddle
JavaScript
Please follow this link correct to work on jquery sortable for ajax contents and binding to a update function see this fiddle
Javascript