jQuery hide() 不起作用
我试图使用 hide 来隐藏下拉菜单(选择元素),但无法做到。下面是我正在使用的 jQuery 代码。
$('.myClass').find('select[name=xyz]').hide();
chrome 中的异常:对象 xyz 没有方法“隐藏”
相应的 HTML
<div class="myClass">
<select id="xyz" name="xyz">
<option>option1</option>
<option>option2</option>
</select>
<select id="123" name="123">
<option>option3</option>
<option>option4</option>
</select>
</div>
I am trying to hide a dropdown (select element) using hide but not able to. Below is the jQuery code I am using.
$('.myClass').find('select[name=xyz]').hide();
Exception in chrome : object xyz has no method 'hide'
Corresponding HTML
<div class="myClass">
<select id="xyz" name="xyz">
<option>option1</option>
<option>option2</option>
</select>
<select id="123" name="123">
<option>option3</option>
<option>option4</option>
</select>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
编辑:我发现您现在已经编辑了帖子中的 HTML,并添加了最初缺少的
name
属性。您的 HTML 和 javascript 在 this jsFiddle 中对我有用,所以如果它对您不起作用,那是因为您的页面中存在其他问题或重复的 ID 或类似的内容。即便如此,我建议您使用下面我建议的更简单的形式,因为它更简单、更快、更万无一失。编辑OP之前的上一个答案:
我没有看到您可以匹配的
name
属性。如果您想获取带有xyz
id 的select
标签,请使用:ID 在页面中是唯一的,因此您可以直接寻址它,无需执行此操作所有其他东西都可以找到它。如果您这样做是因为您的 ID 在页面中不是唯一的,那么您必须修复该问题,因为您将在具有冲突 ID 的不同浏览器中得到不可预测的结果。
EDIT: I see that you have now edited the HTML in your post and added the
name
attribute that was originally missing. Your HTML and javascript works for me in this jsFiddle so if it isn't working for you then it's because you have something else wrong in your page or duplicate IDs or something like that. Even so, I suggest you just use the simpler form that I suggest below as it's simpler, faster and more foolproof.Previous Answer Before OP was edited:
I see no
name
attribute for you to match. If you want to get theselect
tag with thexyz
id, then use this:IDs much be unique in the page so you can just address it directly, no need to do all the other stuff to find it. If you were doing it the way you were because your IDs weren't unique in the page, then you have to fix that as you will get unpredictable results in different browsers with conflicting IDs.
没有
[name=xyz]
将其更改为 id
http://sandbox .phpcode.eu/g/1acce/1
there's no
[name=xyz]
change it to id
http://sandbox.phpcode.eu/g/1acce/1
您的选择中没有姓名,
如果您打算提交该姓名,则必须添加该姓名。最后,注意:ID 和属性一般不应以数字开头,而应以字母开头。
you have no name in your select
you have to add the name if you ever plan to submit that. Final, note: IDs and attributes in general shall not start with a number, but with a letter.
您有标记为 xyz 的选择的 id。
或者更好的是
jQuery [attribute=value] 选择器搜索特定属性。
jQuery id 选择器 将搜索元素的 id。
You have the id of the select labeled xyz.
Or Better yet
The jQuery [attribute=value] selector searches for the specific attribute.
The jQuery id selector will search the id of the element.