无法获取DOM元素值

时间:2014-05-09 16:28:37

标签: javascript jquery html dom handlebars.js

我已经加载了一个HTML页面并在控制台上运行了一些javascript代码。任何人都可以向我解释这种行为。

document.getElementById('tiers__0__.threshold1')给了我

<input id=​"tiers__0__.threshold1" type=​"text" class=​"field  mandatory" data-name=​"tiers__0__.threshold1" data-domain=​"ANY" name=​"dynamicValues['tiers__0__.threshold1']​" value=​"5" maxlength readonly>​

document.getElementById('tiers__0__.threshold1').value给了我

""

document.getElementById('tiers__1__.threshold1')给了我

<input id=​"tiers__1__.threshold1" type=​"text" class=​"field " data-name=​"tiers__1__.threshold1" data-domain=​"ANY" name=​"dynamicValues['tiers__1__.threshold1']​" value=​"10" maxlength readonly>​

document.getElementById('tiers__1__.threshold1').value给了我

"10"

为什么第一个DOM元素没有给我价值。两个DOM元素都具有相同的结构。我使用Handlebars“each”属性生成它们。

P.S。我知道有时ID中的DOT会产生问题。甚至尝试使用转义序列(//)。没有任何效果。尝试Jquery也

$('#tiers__0__\\.threshold1').val()(使用双斜线。所以只显示1)给我

""

问题经过更多研究后更新。

之前的测试是在Chrome浏览器上进行的,但在Firefox上进行测试后得到了不同的结果。

$( '#tiers__0 __。阈值1')

Object[input#tiers__0__.threshold1.field property value = "" attribute value = "5"]

任何人都可以解释什么是属性值以及它为什么只为我的第一个文本框添加为null。他们是否可以使我的属性值相同,或者我可以完全删除它。

注意 - :我使用手柄模板渲染JSON对象。我检查过JSON和Handlebar都包含统一数据。我有一个看起来像这样的输入标签

<input id="{{name}}{{#if count}}{{count}}{{/if}}" type="text" class="field {{#if this.mandatory}} mandatory{{/if}}" data-name="{{name}}" data-domain="{{domain}}" name="{{bindingName}}" value={{val}} maxlength="{{this.maxLength}}" {{#if this.locked}}readOnly{{/if}}/> 

它为for循环中的其他值提供了完美的结果,只是将第一个带有额外的属性

0 个答案:

没有答案