为什么Jquery没有识别像“houseNumber [x]”这样的id的元素

时间:2015-12-11 12:18:45

标签: javascript jquery html

我有一个表格,你必须输入House的属性,如姓名,号码等。 在相同的形式中,他们会提到您需要进入这些房产的房屋数量。根据房屋的数量,我为每个房屋生成输入字段。

#initialize
temp=AAAAA
check=0

#loop per line
while read line
do
type=${line:20:1}
pod=${line:0:2}
date=${line:9:5}

if [ "$type" != "2" ]
then
    echo "$line" >> outfile
    check=1         #if condition true then flag=1
fi

if [ "$pod" == "$temp" ]
then
    if [ $check -ne 1 ]  #if flag=1 dont print - otherwise it prints twice
    then
    echo "$line" >> outfile
    fi
fi

temp=$pod
check=0
done<$1

之后当我尝试读取这样的值时

   var text1 = '<input type="text" min="1" id="houseNo[' + i + ']" class="Houseno form-control" name="projectConfigurationDetails[' + i + '].houseNo">';

它给了我 $('#houseNo[0]').attr('name'); 经过一些跟踪和错误后,我们通过删除方括号将undefined更改为id。然后

"houseNo' + i + '"

这是给予完美的价值。正方形括号让Jquery认为这是一些选择器模式,但我们提到$('#houseNo0').attr('name'); 所以这是#

如果我错过了一些东西请教育我。

2 个答案:

答案 0 :(得分:4)

由于[]是元字符,您需要使用\\转义元字符

$('#houseNo\\[0\\]').attr('name');

Docs

  

使用任何元字符(例如!&#34;#$%&amp;&#39;()* +,。/:;&lt; =&gt;?@ [] ^`{| }〜)作为名称的文字部分,必须使用两个反斜杠进行转义:\\。

答案 1 :(得分:2)

如果您对使用数组没兴趣,请考虑将来使用下划线(houseNo_3)。