使用jQuery按名称定位输入字段 [英] Target input field by name using jQuery
问题描述
我有一个看起来像这样的输入字段:
I have an input field that looks like this:
<input type="hidden" name="submitted[event_email]" value="">
我试图更改它的值,并尝试了以下方法:
I was trying to change the value of it and tried this:
jQuery('submitted[event_email]').val('test');
我想以名称为目标,但是我不确定自己做错了什么.有人可以帮忙吗?
I would like to target it by the name, but i am not sure what i did wrong. Would anyone be able to help?
推荐答案
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: \\
因此您可以这样做:
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');
这篇关于使用jQuery按名称定位输入字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!