admin管理员组文章数量:1333710
I've got a simple HTML form and for some reason jQuery cannot find the element I'm looking for.
HTML:
<form id="form">
<fieldset>
<table>
<tr class="row">
<td class="label">Street</td>
<td class="field"><input type="text" size="50" value="" id="s.street"></td>
</tr>
</table>
</fieldset>
<fieldset>
<tr class="row">
<td class="label">Street</td>
<td class="field"><input type="text" size="50" value="" id="b.street"></td>
</tr>
</table>
</fieldset>
</form>
jQuery :
$(document).ready(
function() {
$("input[id='s.street']").keyup(function() {
$('#b.street').val($(this).val());
});
});
I get no errors in the console log.
I've got a simple HTML form and for some reason jQuery cannot find the element I'm looking for.
HTML:
<form id="form">
<fieldset>
<table>
<tr class="row">
<td class="label">Street</td>
<td class="field"><input type="text" size="50" value="" id="s.street"></td>
</tr>
</table>
</fieldset>
<fieldset>
<tr class="row">
<td class="label">Street</td>
<td class="field"><input type="text" size="50" value="" id="b.street"></td>
</tr>
</table>
</fieldset>
</form>
jQuery :
$(document).ready(
function() {
$("input[id='s.street']").keyup(function() {
$('#b.street').val($(this).val());
});
});
I get no errors in the console log.
Share Improve this question asked Dec 15, 2012 at 3:40 GeoffGeoff 4174 silver badges14 bronze badges 1-
1
$('#b.street')
says find me the element with the id of "b" and it also must have a class of "street". Read the docs on jQuery selectors. – James Kleeh Commented Dec 15, 2012 at 3:46
6 Answers
Reset to default 4If the element HAS to have that exact id, use:
$('#b\\.street')
ID's should be unique, so you shouldn't have to filter with the input tag. Additionally, you need to add an escape sequence before the .
in your ID name. $('#s\\.street')
is the correct selector. I would actually suggest not using the .
...
your id names conflict with the jquery selector syntax.
When defining an id for an element do not use the # . or any spaces within your ID
YOu have a problem in your naming convention. replace the .
in the name to a hyphen.
So:
<input type="text" size="50" value="" id="s.street">
Bees
<input type="text" size="50" value="" id="s-street">
and you select in with jquery like so:
$("#s-street");
Sizzle (the javascript selector library that jQuery implements) will see the s.street
as an element s
or b
with a class of street
. as opposed to an element with id of s.street
.
from here: What are valid values for the id attribute in HTML? it looks like jquery has trouble with ids that have periods and colons. so try removing those and see if it works.
本文标签: javascriptjQuery not finding element using idStack Overflow
版权声明:本文标题:javascript - jQuery not finding element using id - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742268572a2443869.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论