为什么Jquery没有识别出像“houseNumber [x]”这样的id的元素。 [英] why Jquery is not recognizing elements with ids like "houseNumber[x]"

查看:82
本文介绍了为什么Jquery没有识别出像“houseNumber [x]”这样的id的元素。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格,您必须输入House的属性,例如姓名,电话号码等。
在相同的表格中,他们会提及您需要输入这些属性的房屋数量。基于数字我为每个房子生成输入字段。

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

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

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

它给我 undefined
在发现一些线索和错误后,我们通过删除方括号将 id 更改为houseNo'+ i +'。然后

  $('#houseNo0')。attr('name'); 

这就给出了完美的价值。让Jquery认为这是一些选择器模式,但我们是提及,因此 id 。



如果我 是一个元字符,您需要转义元字符使用 \\

  $( '#houseNo\\ [0\\]')ATTR( '姓名'); 

文档


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



I have a form where you have to enter properties of House, like name,number etc. In the same form they will mention for how many number of houses you have to enter these properties.Based on the number of house i'm generating the input fields for each house like this.

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

After that when i'm trying read value like this

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

its giving me undefined After some trail and errors we changed id to "houseNo' + i + '" by removing square brackets.Then

$('#houseNo0').attr('name');

That is giving perfect value.Are square Brackets making Jquery think like that is some selector pattern but we are mentioning # so that is id.

If i'm missing some thing please educate me.

解决方案

As [] is a meta characters, You need to escape meta-characters use \\

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

Docs

To use any of the meta-characters ( such as !"#$%&'()*+,./:;<=>?@[]^`{|}~ ) as a literal part of a name, it must be escaped with with two backslashes: \\.

这篇关于为什么Jquery没有识别出像“houseNumber [x]”这样的id的元素。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆