使用选择器加速此 jQuery 代码
我为许多对象的列表编写了一个过滤器。用户可以通过单选按钮选择他们的选项。
在我的过滤器功能中,我选择两个单选组/集的选中单选按钮:
if ( $('#filter input[name="interval"]:checked').val() == 'all' ) {
if( $('#filter input[name="day"]:checked').val() == 'all' ) {
var searchSelector = "li";
} else {
var searchSelector = "li[data-day=" + $('#filter input[name="day"]:checked').val() + "]";
}
} else {
if( $('#filter input[name="day"]:checked').val() == 'all' ) {
var searchSelector = "li[data-interval=" + $('#filter input[name="interval"]:checked').val() + "]";
} else {
var searchSelector = "li[data-interval=" + $('#filter input[name="interval"]:checked').val() + "]" + "li[data-day=" + $('#filter input[name="day"]:checked').val() + "]";
}
}
正如 jQuery 性能规则 我应该缓存 jQuery 对象。通常这种做法总是有效的 - 但在这种情况下,我的第二个单选按钮的值总是输出第一个检查值的值。
有什么想法,我如何简化这个 jQuery 代码?
谢谢
这是按钮的 HTML:
<form id="filter">
<fieldset>
<label>
<input type="radio" name="interval" value="all">
all</label>
<label>
<label>
<input type="radio" name="interval" value="hourly">
hourly</label>
<label>
<input type="radio" name="interval" value="daily">
daily</label>
<label>
<input type="radio" name="interval" value="weekly">
weekly</label>
<label>
<input type="radio" name="interval" value="monthly">
monthly</label>
<label>
<input type="radio" name="interval" value="yearly">
yearly</label>
</fieldset>
<fieldset>
<label>
<input type="radio" name="day" value="all">
all</label>
<label>
<input type="radio" name="day" value="monday">
monday</label>
<label>
<input type="radio" name="day" value="tuesday">
tuesday</label>
<label>
<input type="radio" name="day" value="wednesday">
wednesday</label>
<label>
<input type="radio" name="day" value="thursday">
thursday</label>
<label>
<input type="radio" name="day" value="friday">
friday</label>
</fieldset>
</form>
这就是我的列表的样子:
<li data-day="monday" data-interval="weekly">foo</li>
<li data-day="friday" data-interval="yearly">foo</li>
i wrote a filter for a list of many objects. Users can choose their options in via radio-buttons.
Within my filter functionality i select the checked radio buttons of both radio groups/sets:
if ( $('#filter input[name="interval"]:checked').val() == 'all' ) {
if( $('#filter input[name="day"]:checked').val() == 'all' ) {
var searchSelector = "li";
} else {
var searchSelector = "li[data-day=" + $('#filter input[name="day"]:checked').val() + "]";
}
} else {
if( $('#filter input[name="day"]:checked').val() == 'all' ) {
var searchSelector = "li[data-interval=" + $('#filter input[name="interval"]:checked').val() + "]";
} else {
var searchSelector = "li[data-interval=" + $('#filter input[name="interval"]:checked').val() + "]" + "li[data-day=" + $('#filter input[name="day"]:checked').val() + "]";
}
}
As it's said in the jQuery Performance Rules i should cache jQuery objects. Usually this practice always works — but in this case the values of my second radio buttons always outputs the value of the first checked value.
Any ideas, how i can simplify this jQuery code?
THANKS
Here is the HTML for the buttons:
<form id="filter">
<fieldset>
<label>
<input type="radio" name="interval" value="all">
all</label>
<label>
<label>
<input type="radio" name="interval" value="hourly">
hourly</label>
<label>
<input type="radio" name="interval" value="daily">
daily</label>
<label>
<input type="radio" name="interval" value="weekly">
weekly</label>
<label>
<input type="radio" name="interval" value="monthly">
monthly</label>
<label>
<input type="radio" name="interval" value="yearly">
yearly</label>
</fieldset>
<fieldset>
<label>
<input type="radio" name="day" value="all">
all</label>
<label>
<input type="radio" name="day" value="monday">
monday</label>
<label>
<input type="radio" name="day" value="tuesday">
tuesday</label>
<label>
<input type="radio" name="day" value="wednesday">
wednesday</label>
<label>
<input type="radio" name="day" value="thursday">
thursday</label>
<label>
<input type="radio" name="day" value="friday">
friday</label>
</fieldset>
</form>
And this is what my list looks like:
<li data-day="monday" data-interval="weekly">foo</li>
<li data-day="friday" data-interval="yearly">foo</li>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下应该执行此
示例: http://www.jsfiddle.net/gaby/xPMn5/
The following should do it
example: http://www.jsfiddle.net/gaby/xPMn5/
为什么不做这样的事情 - 我假设
searchSelector
稍后在代码中应用于.find()
因为没有ul< /code> 定义为在内部查看...
我还假设您正在使用它以某种方式切换这些
的可见性...在第一行中,您可以附加
. hide()
到链并在此代码块的末尾调用$results.show()
Why not do something like this -- I am assuming that the
searchSelector
is being applied to a.find()
later in the code as there is noul
defined to look within...I am also assuming you are using this to toggle the visibility of these
<li>
somehow... In the first line you could attach.hide()
to the chain and call$results.show()
at the end of this block of code