如何用jQuery提交数据?
我有多个 table.sortables
,每个表中都有多个链接。使用 jQuery 我完成了以下操作。
- 修改每一行以添加一个带有 span.grab 的表格单元格。
- 在表格上实现了 jQuery 可排序。
- 当一行被删除时。将出现一个链接,询问用户是否要保存。
- 当用户单击该链接时,我捕获它以准备向 php 脚本发送 ajax 请求。
这就是我被困住的地方。
每个 tr 上都有链接 id。
<tr id="link1"> ..</tr>
每个表也有自己的 id。
<table class="sortable" id="group1">
问题是..如何从 html 中提取这些数据,所以在我的 PHP 脚本中我最好得到这样的东西。
$_POST['link_order']['group1'] = '1|2|3';
$_POST['link_order']['group2'] = '4|5|6';
上面的任何变体都可以,我还可以修改 HTML。我只需要向 PHP 发送菜单。
我就是从这个开始的。
$('table.sortable').each(function() {
});
这就是我陷入困境的地方,因为我不知道数据是否应该进入对象或字符串或什么。有人这样做过吗?
I have a multiple table.sortables
with a number of links in each one. Using jQuery I have done the following.
- Modified each row to add a table cell with a span.grab in it.
- Implemented jQuery sortable on the tables.
- When a row is dropped. A link appears asking whether the user would like to save.
- When the user clicks the link I catch it in preparation to send an ajax request to a php script.
This is where I am stuck.
Each tr has the link id on it..
<tr id="link1"> ..</tr>
Each table has its own id as well.
<table class="sortable" id="group1">
The question is.. how to pull this data from the html so in my PHP script I preferably get something like this.
$_POST['link_order']['group1'] = '1|2|3';
$_POST['link_order']['group2'] = '4|5|6';
Any variation of the above is fine, I can also modify the HTML. I just need to send PHP the menus.
I started with this.
$('table.sortable').each(function() {
});
This is where I got stuck as I didn't know whether the data should go into an object or a string or what. Has anyone done this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用可排序小部件的 serialize() 方法。根据文档:
因此,如果您向
id
属性添加下划线字符(link_1
而不是link1
),serialize()
默认情况下会生成一个$POST['link']
参数。You can use the serialize() method of the sortable widget. According to the documentation:
So, if you add an underscore character to your
id
attributes (link_1
instead oflink1
),serialize()
will generate a$POST['link']
parameter by default.查看 serialize 和 toArray 可排序方法。
Serialize 将为您提供
foo[]=1&foo[]=5&foo[]=2
toArray 将为您提供一个项目数组,然后您可以将其转换为您自己的 url 参数。
Check out the serialize and toArray methods for the sortable.
Serialize will give you
foo[]=1&foo[]=5&foo[]=2
toArray will give you an array of the items that you can then convert in to your own url parms.