通过ID选择隐藏的表单元素 [英] Selecting hidden form element by Id
问题描述
我试图通过选择其ID而不是其name属性来为隐藏的表单元素设置值.隐藏的元素具有id="user_lat
和name="user_lat"
.我该怎么办?
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());
我下面的按ID选择的尝试无效:
MY attempt at selecting by id below does not work:
$("input #user_lat").val(results[0].geometry.location.lat());
推荐答案
如果将id应用于输入,则选择器不能有空格:
If the id is to be applied to the input, the selector can have no spaces:
$("input#user_lat").doSomething();
如果在input
和#user_lat
之间放置一个空格,选择器将尝试匹配输入的子项,这没有多大意义.就像具有以下标记:
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>
删除空格将匹配包含ID的任何输入:
Removing the space matches any input that contains the ID:
<input id="user_lat" />
这篇关于通过ID选择隐藏的表单元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!