如果对象类不包含值,jquery 隐藏对象
标题可能有点令人困惑,基本上我正在尝试编写一个脚本,它将从下拉表单中获取选定的值,并隐藏 li 的类,如果它的类不包含下拉表单中的值..如果那样的话有道理吗??每个 li 有多个类..
这是我到目前为止所得到的(抱歉,如果它很粗糙):
FORM:
<form action="" name="filter" id="filter" method="post">
<select name="bedrooms">
<option value="">- Select Bedrooms -</option>
<option value="bed-1">1</option>
<option value="bed-2">2</option>
<option value="bed-3">3</option>
<option value="bed-4">4</option>
<option value="bed-5">5</option>
</select>
<select name="bathrooms">
<option value="">- Select Bathrooms -</option>
<option value="bath-1">1</option>
<option value="bath-2">2</option>
<option value="bath-3">3</option>
<option value="bath-4">4</option>
<option value="bath-5">5</option>
</select>
<select name="frontage">
<option value="">- Select Frontage Size -</option>
<option value="frontage-100">100</option>
<option value="frontage-200">200</option>
<option value="frontage-300">300</option>
<option value="frontage-400">400</option>
<option value="frontage-500">500</option>
</select>
<input type="submit" name="filter-submit" id="filter-submit" value="Go" />
</form>
JQuery:
$("#filter-submit").click(function() {
var foo = [];
$("#filter :selected").each(function(i, value){
foo[i] = $(value).val();
});
if (foo) {
$.each(foo, function(index, value){
if (value) {
//hide other objects based on "value"
//$("#portfolio li").hasClass();
};
});
};
return false;
});
好的,所以我陷入困境的是如何隐藏所有没有的“#portfolio li”输出为“值”的类。我想我可以使用 hasClass 但不知道如何扭转它..如果这有意义吗?任何帮助将不胜感激:)
The title might be a bit confusing, basically I'm trying to write up a script which will grab the selected values from a drop down form, and hide li's if its class does not contain the values from the drop down form.. if that makes sense?? Each li has multiple classes..
Here's what I've got so far (sorry if it's crude):
FORM:
<form action="" name="filter" id="filter" method="post">
<select name="bedrooms">
<option value="">- Select Bedrooms -</option>
<option value="bed-1">1</option>
<option value="bed-2">2</option>
<option value="bed-3">3</option>
<option value="bed-4">4</option>
<option value="bed-5">5</option>
</select>
<select name="bathrooms">
<option value="">- Select Bathrooms -</option>
<option value="bath-1">1</option>
<option value="bath-2">2</option>
<option value="bath-3">3</option>
<option value="bath-4">4</option>
<option value="bath-5">5</option>
</select>
<select name="frontage">
<option value="">- Select Frontage Size -</option>
<option value="frontage-100">100</option>
<option value="frontage-200">200</option>
<option value="frontage-300">300</option>
<option value="frontage-400">400</option>
<option value="frontage-500">500</option>
</select>
<input type="submit" name="filter-submit" id="filter-submit" value="Go" />
</form>
JQuery:
$("#filter-submit").click(function() {
var foo = [];
$("#filter :selected").each(function(i, value){
foo[i] = $(value).val();
});
if (foo) {
$.each(foo, function(index, value){
if (value) {
//hide other objects based on "value"
//$("#portfolio li").hasClass();
};
});
};
return false;
});
Ok so where I'm stuck is how to hide all "#portfolio li"'s which don't have the class which is outputted as "value". I thought I could use hasClass but not sure how to reverse it.. if that makes sense? Any help would be appreciated :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
.not()
并传递该类,如下所示:You could use
.not()
and pass the class, like this:只是想我会把这个扔出去好玩。采用稍微不同的方法来隐藏
元素。在一个选择器中完成所有操作。
value
变量将保存一个类似这样的字符串:Just thought I'd throw this one out there for fun. Takes a little different approach to hiding the
<li>
elements. Does it all in one selector.The
value
variable will hold a String something like this:试一试吧!
give that a go!