使用 JQuery 隐藏选择的选项
好的,让我们从一个例子开始。
请记住,这只是一个示例。
现在,我们有一个包含 3 个选项的下拉菜单。
我现在想做的是隐藏一个选项。
添加 style = "display:none"
将没有帮助。
该选项不会出现在下拉列表中,但使用箭头键,您仍然可以选择它。
本质上,它完全按照代码所述执行。它没有显示,就停在那里。
$("#1").hide()
的 JQuery 函数将不起作用。
另外,我不仅想隐藏该选项,还想完全删除它。
有这样做的可能性吗?
我必须使用父/同级/子元素吗?如果是这样,我仍然不确定如何。
一个相关的问题:我发现JQuery中有一个.remove()
可用。效果很好。
但如果我想把它带回来怎么办?
如果(条件)
{
$(this).remove();
}
我可以循环这个。不应该很复杂。
但我想做的是:
类的最大容量:(此处输入字段)
选择房间:(此处为下拉菜单)
我希望它做的是使用 .change()
或 .keyup
等函数更新 Dropdown >.
我只能在输入内容后创建下拉菜单。在更改或按键时,相应地执行下拉菜单。
但我正在做的是这样的:
$roomarray = mysql_query("选择 *
来自
(
选择 *,
案例
当类型=“课堂”时,1
WHEN type = '计算机实验室' THEN 2
WHEN type = '演讲厅' THEN 3
WHEN type = '礼堂' THEN 4
END AS ClassTypeValue
来自房间
) t
ORDER BY ClassTypeValue, maxppl, roomID");
echo "<select id = \"room\">";
while ($rooms = mysql_fetch_array($roomarray))
{ ?>
<option value=<?php echo $rooms['roomID']; ?> id=<?php echo $rooms['roomID']; ?>><?php echo $rooms['type']; echo " "; echo $rooms['roomID']; echo " ("; echo $rooms['maxppl']; echo ")"; ?></option>
<?php
}
echo "</select>";
是的,我知道这很乱。我打算稍后更改它。
但现在的问题是:我可以切换选项的删除吗根据输入的内容
是否可以通过循环执行此操作?因为我肯定无法继续执行 SQL 查询,因为如果可能的话,我仍然认为它是可行的。一个坏的。
Okay, let's start with an example.
Keep in mind, this is only an example.
<select id = "selection1">
<option value = "1" id = "1">Number 1</option>
<option value = "2" id = "2">Number 2</option>
<option value = "3" id = "3">Number 3</option>
</select>
Now from here, we have a dropdown with 3 options.
What I want to do now is to hide an option.
Adding style = "display:none"
will not help.
The option would not appear in the dropdownlist, but using the arrow keys, you can still select it.
Essentially, it does exactly what the code says. It isn't displayed, and it stops there.
A JQuery function of $("#1").hide()
will not work.
Plus, I don't only want to hide the option, I want to completely remove it.
Any possibility on doing so?
Do I have to use parent/sibling/child elements? If so, I'm still not sure how.
A related question: I found out that there is a .remove()
available in JQuery. Works well.
But what if I want to bring it back?
if(condition)
{
$(this).remove();
}
I can loops this. Shouldn't be complicated.
But the thing of which I am trying to do is this:
Maximum Capacity of Class: (Input field here)
Select Room: (Dropdown here)
What I'd like for it to do is to update is Dropdown using a function such as .change()
or .keyup
.
I could create the dropdown only after something is typed. At a change or a keyup, execute the dropdown accordingly.
But what I am doing is this:
$roomarray = mysql_query("SELECT *
FROM
(
SELECT *,
CASE
WHEN type = 'Classroom' THEN 1
WHEN type = 'Computer laboratory' THEN 2
WHEN type = 'Lecture Hall' THEN 3
WHEN type = 'Auditorium' THEN 4
END AS ClassTypeValue
FROM rooms
) t
ORDER BY ClassTypeValue, maxppl, roomID");
echo "<select id = \"room\">";
while ($rooms = mysql_fetch_array($roomarray))
{ ?>
<option value=<?php echo $rooms['roomID']; ?> id=<?php echo $rooms['roomID']; ?>><?php echo $rooms['type']; echo " "; echo $rooms['roomID']; echo " ("; echo $rooms['maxppl']; echo ")"; ?></option>
<?php
}
echo "</select>";
Yes, I know it is very messy. I plan to change it later on.
But the issue now is this: Can I toggle the removal of the options according to what has been typed?
Is it possible to do so with a dropdown made from a loop? Because I sure as hell can't keep executing SQL Queries. Or is that even an option? Because if it's possible, I still think it's a bad one.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
hide() 不适用于 Chrome...我一直在尝试一种解决方法并将其包装在一个名为“ExtraBox”的插件中。这个想法是临时存储选项,以便我们可以通过操作 DOM 来启用/禁用它们。我创建了该插件以使其更易于使用。
我已将代码发布在 jsFiddle 上,因此您可以自己尝试一下: http://jsfiddle.net/KeesCBakker/HaFRC/。 [注意:jsFiddle 中的第一个项目,我很喜欢它!]
Html 示例:
我已将以下 jQuery 添加到我的 document.ready 函数中:
该插件如下所示:
The hide() doesn't work for Chrome... I've been experimenting with a work-around and wrapped it in a plugin I called "ExtraBox". The idea is to store the options temporary so we can enable / disable them by manipulating the DOM. I've created the plugin to make it easier to use.
I've posted the code on jsFiddle, so you can try it for yourself: http://jsfiddle.net/KeesCBakker/HaFRC/. [note: first project in jsFiddle, I'm loving it!]
Html example:
I've added the following jQuery to my document.ready function:
The plugin looks like this:
如果您只想删除元素,为什么要搞乱显示设置呢?
它消失了。
Why are you messing around with display settings if you just want to remove the element?
and its gone.
实际上可以隐藏
节点,但不兼容跨浏览器。
it actually is possible to hide an
<option>
node, but not cross-browser compatible.我想,你的选择器是错误的。尝试这样的操作(已更新):
请参阅父子选择器(父>子)。
I suppose, your selector is wrong. Try something like this (updated):
See parent-child selector (parent > child).
我已经提供了一个 jQuery 插件,可以很好地解决这个问题。有了它,您可以这样做:
您可以根据需要隐藏和显示它们,并且它们将保持原始顺序。它适用于所有浏览器。您可以在此示例中看到: jsFiddle - 切换下拉选项
您可以获得 切换下拉选项插件。如果您不喜欢插件,只需将其中的 JavaScript 代码复制到您自己项目的 JavaScript 文件中即可。有关更多信息,请参阅插件上的阅读文档链接!
I've made available a jQuery Plugin that solves this very nicely. With it, you would do this:
You can hide and show them as much as you want, and they will keep their original order. It works in all browsers. You can see that in this sample: jsFiddle - Toggle Dropdown Options
You can get the Toggle Dropdown Options plug-in. If you don't like plug-ins, just copy the JavaScript code from it to your own project's JavaScript file. See the Read the Docs link on the plug-in for more information!
这在 Jquery 中非常简单
This Is Very Simple In Jquery