如何添加字符串到jQuery选择器? [英] How to add string to jQuery selector?

查看:180
本文介绍了如何添加字符串到jQuery选择器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试选择在同一网页的不同帖子上更改的ID。因此,他们被赋予了id =show_posts_ {PostID} - 在最终输出中,{PostID}被替换为一个数字。在函数中,我需要调用 $('show_posts_XXXXXX') - XXXXXX是生成的ID。我已经将这个ID存储在一个变量postId。

I'm trying to select an id that changes on different posts of same page. So the they have been given an id="show_posts_{PostID}" - on the final output the {PostID} is replaced with a number. In the function I need to call $('show_posts_XXXXXX') - XXXXXX being the generated ID. I have stored this ID in a variable called postId.

但我不能这样做 $('+show_posts_ + postId +')

任何人都可以告诉我如何在选择器的末尾添加一个字符串?

Can anyone tell me how I can add a string to the end of a selector?

推荐答案

应该工作。如果是,你会踢你自己。不要忘记ID的哈希,并且不需要额外的引号。

Should work. If it does, you'll kick yourself. Don't forget the hash for the ID, and the extra quotation marks aren't necessary.

$("#show_posts_" + postId)

这篇关于如何添加字符串到jQuery选择器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆