开发者

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. The hidden element has id="user_lat and name="user_lat". How can I do that?

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:

开发者_运维知识库$("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:

$("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. It would be like having the following markup:

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

Removing the space matches any input that contains the ID:

<input id="user_lat" />


You must stick them together "input#user_lat"

input #user_lat means:
Look for an input and then find inside the element with id user_lat


You are close, take out "input" from the second statement and you should be good.

$("#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. So what you need to do is just delete the space between them, something like this:

$("input#user_lat") ...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜