如何用jQuery提交数据?

发布于 2024-11-25 18:56:43 字数 783 浏览 3 评论 0原文

我有多个 table.sortables,每个表中都有多个链接。使用 jQuery 我完成了以下操作。

  1. 修改每一行以添加一个带有 span.grab 的表格单元格。
  2. 在表格上实现了 jQuery 可排序。
  3. 当一行被删除时。将出现一个链接,询问用户是否要保存。
  4. 当用户单击该链接时,我捕获它以准备向 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.

  1. Modified each row to add a table cell with a span.grab in it.
  2. Implemented jQuery sortable on the tables.
  3. When a row is dropped. A link appears asking whether the user would like to save.
  4. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

西瑶 2024-12-02 18:56:43

您可以使用可排序小部件的 serialize() 方法。根据文档:

默认情况下,它通过查看格式中每个项目的 id 来工作
'setname_number',它会输出一个像这样的哈希值
“setname[]=数字&setname[]=数字”。

您还可以提供选项哈希作为自定义定义的第二个参数
该功能如何工作。可能的选项有: 'key'(替换
part1[] 随意),'attribute'(测试另一个属性
比“id”)和“表达式”(使用您自己的正则表达式)。

因此,如果您向 id 属性添加下划线字符(link_1 而不是 link1),serialize()默认情况下会生成一个 $POST['link'] 参数。

You can use the serialize() method of the sortable widget. According to the documentation:

It works by default by looking at the id of each item in the format
'setname_number', and it spits out a hash like
"setname[]=number&setname[]=number".

You can also give in a option hash as second argument to custom define
how the function works. The possible options are: 'key' (replaces
part1[] with whatever you want), 'attribute' (test another attribute
than 'id') and 'expression' (use your own regexp).

So, if you add an underscore character to your id attributes (link_1 instead of link1), serialize() will generate a $POST['link'] parameter by default.

葵雨 2024-12-02 18:56:43

查看 serializetoArray 可排序方法。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文