Dynamic Variable Selectors in JQuery

后端 未结 1 1578
滥情空心
滥情空心 2020-12-20 06:29

If I have a varibale that changes on certain conditions how do I select it using Jquery selectors?

var id = 2
alert($(\'.\'+id+ \' .title\').val());
<         


        
相关标签:
1条回答
  • 2020-12-20 07:30

    your problem here is that you are starting a classname with a number. class names must start with an _ a - or a letter.

    switch up your structure so its:

    var id = 2
    alert($('.' + 'title' + id).val());
    

    or some variation so that id starts with an underscore, dash, or letter:

    var id = 2
    alert($('._' + id + ' .title').val());
    
    0 讨论(0)
提交回复
热议问题