Target input field by name using jQuery

前端 未结 2 1930
伪装坚强ぢ
伪装坚强ぢ 2021-01-20 15:31

I have an input field that looks like this:


I was trying to change

相关标签:
2条回答
  • 2021-01-20 16:17

    You need to use attribute equals selector [name="value"]

    Also from the docs:

    To use any of the meta-characters ( such as !"#$%&'()*+,./:;<=>?@[]^`{|}~ ) as a literal part of a name, it must be escaped with with two backslashes: \\

    So you can do:

    jQuery('input[name="submitted\\[event_email\\]"]').val('test');
    

    But actually you don't need to escape the [ ] here since it's wrapping in quotes:

    jQuery('[name="submitted[event_email]"]').val('test');
    
    0 讨论(0)
  • 2021-01-20 16:19

    You need to to escape [ and ] using \ but \ is also special character so we escape it also.

    so we escape using \\

    jQuery('[name="submitted\\[event_email\\]"]').val('test');
    

    Attribute Equals Selector [name="value"]


    Update

    Fiddle Demo

    There is no need to escape [ and ] as it is wrapped in quotes (i.e It is string)

    jQuery('[name="submitted[event_email]"]').val('test');
    //            ^                      ^
    
    0 讨论(0)
提交回复
热议问题