Jquery - 访问特定于每一行的变量?

发布于 2024-12-11 20:06:58 字数 1344 浏览 0 评论 0原文

我有一个网页并使用 php 我查询数据库并将结果显示在表格中,如下所示:

<table id="tablelist">
<thead>
<tr>
<th>Header1</th>
<th>Header2</th>
<th>Header3</th>
</tr>
</thead>
<tbody>
<tr>
<td>Info here1</td>
<td>Info here1</td>
<td class="actions">Action1 - Action2 - Action3</td>
</tr>
<tr>
<td>Info here2</td>
<td>Info here2</td>
<td class="actions">Action1 - Action2 - Action3</td>
</tr>
</tbody>
</table>

现在,每一行都有来自数据库的特定于该行的唯一信息。例如主 ID、日期和类别。当有人单击任何行的操作时,我需要获取特定于他们单击的行的这两条信息。目前我尝试这样做:

<div class="row_data">{info2 in json format}</div>    
<tr>
    <td>Info here1</td>
    <td>Info here1</td>
    <td class="actions">Action1 - Action2 - Action3</td>
    </tr>
<div class="row_data">{info2 in json format}</div>    
<tr>
    <td>Info here2</td>
    <td>Info here2</td>
    <td class="actions">Action1 - Action2 - Action3</td>
    </tr>

因此,当有人单击操作链接时,它将获取 row_data 的先前值,该值采用 json 格式。我将使用 jquery 解析 json 值并使用在 jquery 代码的其余部分中找到的信息。但我被告知这是无效的 html,不推荐。单击某个操作时使用的每行记录动态数据的推荐/最佳方式是什么,以便我可以在 jquery 代码中引用它?

I have a web page and using php Im quering a database and displaying the results in a table like so:

<table id="tablelist">
<thead>
<tr>
<th>Header1</th>
<th>Header2</th>
<th>Header3</th>
</tr>
</thead>
<tbody>
<tr>
<td>Info here1</td>
<td>Info here1</td>
<td class="actions">Action1 - Action2 - Action3</td>
</tr>
<tr>
<td>Info here2</td>
<td>Info here2</td>
<td class="actions">Action1 - Action2 - Action3</td>
</tr>
</tbody>
</table>

Now each row has unique information specific to that row that is from the database. For example the primary id, a date and category. When someone clicks an action for any of the rows I need to obtain those 2 pieces of information specific to the row they clicked. Currently I tried doing this:

<div class="row_data">{info2 in json format}</div>    
<tr>
    <td>Info here1</td>
    <td>Info here1</td>
    <td class="actions">Action1 - Action2 - Action3</td>
    </tr>
<div class="row_data">{info2 in json format}</div>    
<tr>
    <td>Info here2</td>
    <td>Info here2</td>
    <td class="actions">Action1 - Action2 - Action3</td>
    </tr>

So when someone clicked on an action link it would get the previous value of row_data, which is in json format. I would use jquery to parse the json value and use the information found within the rest of the jquery code. But I was told this is invalid html and not recommended. What would be a recommended/optimal way of storying dynamic data per row to use when an action is clicked so I can reference it in my jquery code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

╰つ倒转 2024-12-18 20:06:58

方法是使用 HTML 5 的数据属性:

<tr id="foo" data-something="put whatever you want in here">
    <!-- content -->
</tr>

然后您可以使用 data-something 的值访问您存储在其中的信息

$("#foo").data("something")

,可以是您喜欢的任何值(只要它经过 过滤) htmlspecialchars(如果您直接从 PHP 回显它),并且如果更方便的话您可以使用多个数据属性(通常是这样)。

还有一篇关于该主题的 John Resig 的帖子

The way to do it is use HTML 5's data attributes:

<tr id="foo" data-something="put whatever you want in here">
    <!-- content -->
</tr>

You can then access the information you stored there with

$("#foo").data("something")

The value of data-something can be anything you like (as long as it's filtered through htmlspecialchars if you are echoing it straight from PHP), and you can use multiple data attributes if that's more convenient (usually it is).

There's also a post by John Resig on the subject.

梦开始←不甜 2024-12-18 20:06:58

Jon 的建议可行,但我讨厌在 HTML 中嵌入数据,因此我会创建一个从 id 到数据的 JSON 映射,并将这些 id 添加到 TR。从点击处理程序中,您可以获取 TR 的 ID 并从地图中查找数据。

Jon's suggestion works, but I hate embedding data in HTML, so I would create a JSON map from id to the data, and add those ids to the TRs. From the click handler, you grab the TR's id and find the data from the map.

恰似旧人归 2024-12-18 20:06:58

我想我累了,可能误解了这一点,所以如果我提出完全错误的建议,请原谅...

所以说有人点击了任意行中的操作 1。也许您会从每个链接上的点击侦听器开始,假设您为链接提供了一个名为“action”的类...

<tr>
    <td class="json_info" style="display:none">{your json string}</td>
    <td>Info 1</td>
    <td>Info 2</td>
    <td><a class="action">action link</a></td>
</tr>

$('.action').click(function(){
    var json = $(this).parent().find('.json_info').html();
});

如果 json_info 的 html 是格式正确的 json 字符串,那么它应该能够从中读取为 json jquery 选择调用。

I think i'm tired and may have misunderstood this so please forgive if I suggest something totally wrong...

So say some one clicks action 1 in any row. Perhaps you would begin with a click listener on each link assuming you give your links a class called "action" ...

<tr>
    <td class="json_info" style="display:none">{your json string}</td>
    <td>Info 1</td>
    <td>Info 2</td>
    <td><a class="action">action link</a></td>
</tr>

$('.action').click(function(){
    var json = $(this).parent().find('.json_info').html();
});

If the html for json_info is a properly formatted json string then it should be able to be read as json right out of this jquery selection call.

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