jquery - 找到具有特定类的最后一个元素并将其传递给 php?

发布于 2024-08-16 14:47:51 字数 315 浏览 3 评论 0原文

基本上,我有一个列表:

<li class="list">fruit</li>
<li class="list">vegetables</li>
<li class="list">protein</li>

并且,我想找到最后一个列表项,从中获取文本,然后将其分配给 php 变量,这样:

<?php

$variable = 'protein';

?>

我对如何将其放入 php 变量感到模糊?

Basically, I have a list:

<li class="list">fruit</li>
<li class="list">vegetables</li>
<li class="list">protein</li>

And, I want to find the last list item, get the text from it, and then assign it to a php variable, so that:

<?php

$variable = 'protein';

?>

I'm fuzzy on how to get it into a php variable?

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

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

发布评论

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

评论(4

暮光沉寂 2024-08-23 14:47:51

JavaScript / jQuery 方面:

$.post("script.php", { value: $(".list:last").text() }, function (result) {
  alert(result); // alerts whatever comes from the php script
});

PHP 方面

print strtoupper( $_POST["value"] );

The JavaScript / jQuery Side:

$.post("script.php", { value: $(".list:last").text() }, function (result) {
  alert(result); // alerts whatever comes from the php script
});

The PHP Side

print strtoupper( $_POST["value"] );

Javascript 和 PHP 之间存在根本区别:PHP 在服务器端运行,并生成页面代码。 JavaScript 在 PHP 运行并提供内容之后在客户端运行。因此,您无法将某些内容从 JQuery“返回”到 PHP。您必须进行 AJAX 调用这样做。有关如何使用 JQuery 执行此操作的更多信息,请参阅此处

但您想要做的事情听起来很容易仅用 JQuery 即可实现。为什么选择 PHP?

There is a fundamental difference between Javascript and PHP: PHP runs on the server side, and produces the page's code. JavaScript runs on the client side, after PHP has run and served the content. So you can't pass something "back" from JQuery to PHP. You would have to make an AJAX call to do that. More on how to do that with JQuery here.

But what you're trying to do sounds easy enough to achieve in JQuery alone. Why the PHP?

随波逐流 2024-08-23 14:47:51
$.post("script.php", { value: $(".list:last-child").text() }, function(result){
 // Your Code
});
$.post("script.php", { value: $(".list:last-child").text() }, function(result){
 // Your Code
});
七色彩虹 2024-08-23 14:47:51

如果 php 正在创建所有 html,那么每次打印列表项时,您都可以为内容设置一个变量。

<li class="list"><?PHP echo $var; ?></li><?PHP $yourvar = $var; ?>
<li class="list"><?PHP echo $var; ?></li><?PHP $yourvar = $var; ?>
<li class="list"><?PHP echo $var; ?></li><?PHP $yourvar = $var; ?>

列表完成后,您将在变量中看到最后的内容。

如果您从数据库返回结果,那么更有效的方法是找出您返回了多少行。然后用循环遍历时所在行的编号来测试该数字。然后你也可以找到最后一行

if the php is creating all the html, then each time you are printing the list item you could set a variable to the content.

<li class="list"><?PHP echo $var; ?></li><?PHP $yourvar = $var; ?>
<li class="list"><?PHP echo $var; ?></li><?PHP $yourvar = $var; ?>
<li class="list"><?PHP echo $var; ?></li><?PHP $yourvar = $var; ?>

Once the list is finished you will have the last content in a variable.

If you are returning the results from a database then a little more efficient would be finding out how many rows you have returned. then testing that number with the number of the row you are on as you loop through it. You will be able to find the last row then too

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