jQuery 查找并提供值问题

发布于 2025-01-03 20:03:15 字数 543 浏览 1 评论 0原文

代码位于:http://jsfiddle.net/m9yen/

问题:

  • 我有多个 LI 的列表 食品
  • 每个li 包含一个链接(添加),其中 href 是一个 rel,它保存特定的 FOODID
  • 我现在需要一个数量输入框,
  • 我还需要使用下拉菜单选择服务类型(即克等我将填充)
  • 最终问题是单击添加时,我需要获取 href 的 rel、输入值和所选选项仅适用于该特定 LI 和通过ajax将其发送到request.php。

现在我确实让它专门与 rel 一起工作,只有正弦,如果单击 href 获取 attr('rel') 并通过 ajax 将其提交到 request.php - 但是因为这需要 href 之外的项目的其他值,但仅在那个特定的 LI 中,我迷路了,并尝试使用 .find() 但没有运气。

注意:将有多个列表,可能有 50-100 个列表(每个 LI)

Code is at: http://jsfiddle.net/m9yen/

Issue:

  • I have multiple LI's listing FOOD ITEMS
  • Each li contains a link (add) and in that href is a rel which holds that specific FOODID
  • I now need to have an input box for QUANITY
  • I also need to a select with dropdowns for SERVING TYPE (i.e. grams, et which I'll populate)
  • End issue is when the add is clicked, I need to get the rel for the href, the value of the input and the selected option for that specific LI ONLY and sent it to request.php via ajax.

Now I did have it working specifically with the rel only sine it was a if href is clicked get attr('rel') and submit it via ajax to request.php -- BUT since this requires other values of items outside the href but only within that specific LI, I'm lost and have tried using .find() with no luck what so ever.

NOTE: There will be more than one, possible 50-100 listings (each LI)

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

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

发布评论

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

评论(2

臻嫒无言 2025-01-10 20:03:15

由于 inputselect 元素是其兄弟元素,因此您必须使用 siblings 方法来查找它们。由于元素的名称与您要匹配的元素的名称相同,因此请使用 = 而不是 =^= 并仅使用 "rel" 来获取来自锚点的属性值而不是“a#rel”。

var returnedResult = $(this).attr('rel') + "-" + $(this).siblings('input[name="servingAmount"]'.val() + "-" $(this).siblings('select[name="servingType"]').val();

演示

Since input and select elements are its siblings you have to use siblings method to find them. Since the name of the elements are same as what you are matching use = instead of =^= and just use "rel" to get the attribute value from anchor instead of "a#rel".

var returnedResult = $(this).attr('rel') + "-" + $(this).siblings('input[name="servingAmount"]'.val() + "-" $(this).siblings('select[name="servingType"]').val();

Demo

安人多梦 2025-01-10 20:03:15

http://jsfiddle.net/m9yen/3/ - 一般都整理好了。

  • 更短的语法监听 #addING 的点击
  • 使用 event.preventDefault()(而不是 return false)以
  • ,以防止使用 执行链接。 siblings()...val() 获取输入值的
  • =^= 更改为 = - 无论如何,您都会匹配确切的名称。如果您需要“开头为”,只需 ^= 即可

http://jsfiddle.net/m9yen/3/ - generally tidied up.

  • Listening for a click on #addING in shorter syntax
  • using event.preventDefault() instead of return false to prevent link execution
  • using .siblings()...val() to get values of inputs
  • changed =^= to just = - you're matching the exact name anyway. If you need "starting with", just ^= will do
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文