如何选择ID中包含特殊字符的元素? [英] How do I select an element with special characters in the ID?

查看:876
本文介绍了如何选择ID中包含特殊字符的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



 < p>< p>< ; input type =textid =Punteggi [@counter] .Descrizionereadonly =readonlytabindex = -  1name =Punteggi [@counter] .Descrizioneclass ={validate:{required:true }}value =@ item.Descrizione/> 

如何使用jQuery设置这些输入的值,具体而言,我将如何选择它们?我尝试了很多方法,但没有成功。


解决方案

问题是括号在CSS中有特殊含义(作为属性选择器)和点也是类选择器。尝试 $。escapeSelector

  $('#'+ $ .escapeSelector('Punteggi [@counter] .Descrizione'))

这会转义选择器,所以特殊字符不会影响选择。您也可以尝试使用属性选择器并将其用引号括起来:

  $('[id =Punteggi [@counter] .Descrizione]')

这将与该ID完全匹配,并且不会处理任何特殊字符作为特殊字符。


I have a page with a grid containing a lot if input with "strange names" to simulate an array:

 <input type="text" id="Punteggi[@counter].Descrizione" readonly="readonly" tabindex="-1" name="Punteggi[@counter].Descrizione" class="{validate: {required:true}}" value="@item.Descrizione" /> 

How can I set the value of these inputs with jQuery, specifically, how would I select them? I tried in a lot of ways but nothing works.

解决方案

The problem is that brackets have special meaning in CSS (as attribute selectors) and dots do too, as class selectors. Try $.escapeSelector:

$('#' + $.escapeSelector('Punteggi[@counter].Descrizione'))

This will escape the selector so the special characters don't affect the selection. You could also try using an attribute selector and wrapping that in quotes:

$('[id="Punteggi[@counter].Descrizione"]')

This will literally match that ID and won't treat any of the special characters as special characters.

这篇关于如何选择ID中包含特殊字符的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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