jquery selector not working with [ ]

前端 未结 3 1588
南笙
南笙 2021-01-27 11:52

I\'m using jQuery to manipulate form data in an osCommerce instance and I\'m having trouble selecting some elements.

The script generates textareas with the id product_d

相关标签:
3条回答
  • 2021-01-27 12:11
    $("#product_description\\[1\\]").attr("id", "products_description_1");
    

    is the right code, look at "ID-Selector" on jQuery.com

    0 讨论(0)
  • 2021-01-27 12:27

    Try this:

    $('#product_description\\[1\\]')
    

    Note that while this may work, the bracket characters are not valid for use in IDs prior to HTML5 (although they're fine for use in classes).

    0 讨论(0)
  • 2021-01-27 12:33

    The [] characters are not valid ID characters in HTML4. Don't expect consistent results in different browsers if you use them.


    EDIT:

    If you just can't control the format of the IDs on the server side, you could do this:

    $("*[id='product_description[1]']")
    

    but it will be terribly slow in browsers that don't support querySelectorAll.

    (Note that you shouldn't need the \\ here because of the quotation marks around the value.)

    0 讨论(0)
提交回复
热议问题