Target input field by name using jQuery

前端 未结 2 1932
伪装坚强ぢ
伪装坚强ぢ 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');
    

提交回复
热议问题