jQuery-当id为数组类型表示法时,需要有关ID选择器的帮助 [英] jQuery - help needed on ID selector when id is an array type notation

查看:148
本文介绍了jQuery-当id为数组类型表示法时,需要有关ID选择器的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个定义如下的输入元素:

I have an input element defined as follows:

<input type="checkbox" name="custom_15[1]" id="custom_15[1]" value="1" />

当我尝试使用$("#custom_15[1]")选择器时,它不起作用.而document.getElementById("custom_15[1]")确实有效.

When I tried with $("#custom_15[1]") selector, it didn't work. Whereas, document.getElementById("custom_15[1]") did work.

我在做什么错了?

谢谢!

推荐答案

首先,id属性不应包含方括号.这只是无效的.它可以包含字母,数字,下划线,连字符,冒号和点.

First off, an id attribute should not contain square brackets. It's just not valid. It can contain letters, numbers, underscores, hyphens, colons and dots.

在此问题的答案中,有一个暗示,即jquery甚至在点和冒号方面都存在问题:

In the answer to this question there's a hint that jquery even has problems with dots and colons:

什么是ID属性的有效值? HTML?

因此,请尝试切换为有效的ID.如果不能,请使用适当的转义:

So try to switch to valid ids. If you can't, use proper escaping:

$("#custom_15\\[1\\]")

这篇关于jQuery-当id为数组类型表示法时,需要有关ID选择器的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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