简体   繁体   English

通过ID选择隐藏的表单元素

[英]Selecting hidden form element by Id

I am trying to set a value to a hidden form element by selecting their Id and not their name attribute. 我试图通过选择其ID而不是其name属性来为隐藏的表单元素设置一个值。 The hidden element has id="user_lat and name="user_lat" . How can I do that? 隐藏的元素具有id="user_latname="user_lat" 。我该怎么做?

I seem to be able to select by name: 我似乎可以按名称选择:

$("input[name='user_lat']").val(results[0].geometry.location.lat());

MY attempt at selecting by id below does not work: 我尝试通过以下ID进行选择不起作用:

$("input #user_lat").val(results[0].geometry.location.lat());

If the id is to be applied to the input, the selector can have no spaces: 如果将id应用于输入,则选择器不能有空格:

$("input#user_lat").doSomething();

If you place a space between input and #user_lat , the selector attempts to match a child of the input, which doesn't make much sense. 如果在input#user_lat之间放置一个空格,则选择器将尝试匹配输入的子项,这没有多大意义。 It would be like having the following markup: 就像具有以下标记:

<input><el id="user_lat" /></input>

Removing the space matches any input that contains the ID: 删除空格将匹配包含ID的所有输入:

<input id="user_lat" />

You must stick them together "input#user_lat" 您必须将它们粘贴在一起"input#user_lat"

input #user_lat means: input #user_lat表示:
Look for an input and then find inside the element with id user_lat 查找input ,然后在ID为user_lat的元素内user_lat

您很接近,从第二条语句中删除“输入”,您应该会很好。

$("#user_lat").val(results[0].geometry.location.lat());

When you are usign the selector "input #user_lat" your saying the element "user_lat" inside an input. 当您使用选择器“ input #user_lat”时,在输入中说元素“ user_lat”。 So what you need to do is just delete the space between them, something like this: 因此,您需要做的就是删除它们之间的空间,如下所示:

$("input#user_lat") ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM