How to add string to jQuery selector?

后端 未结 4 1734
半阙折子戏
半阙折子戏 2020-12-19 06:02

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 re

相关标签:
4条回答
  • 2020-12-19 06:06

    just use $("#show_posts_"+postID)

    0 讨论(0)
  • 2020-12-19 06:14

    You need to include the '#' character at the start of the string.

    $('#show_posts_' + postId)
    

    Also, you're trying to stuff the quotes in there in your example, and that doesn't make sense.

    0 讨论(0)
  • 2020-12-19 06:17

    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)
    
    0 讨论(0)
  • 2020-12-19 06:22

    Two things.

    1. To select an element with a given ID, you need a # character at the beginning of the ID.

    2. Don't add the quotes to the beginning and end of the string.

    Thus:

    $('#show_posts_' + postId)
    
    0 讨论(0)
提交回复
热议问题