[英]jQuery function to hide elements based on HTML5 data tags not running on Click
I have been working on a small function in jQuery to hide elements based on values in their data tags. 我一直在研究jQuery中的一个小功能,以根据其数据标签中的值隐藏元素。 For example, if the value of bathrooms is 3 hide all elements that do less than 3 bathrooms set in the tag.
例如,如果洗手间的值为3,则隐藏少于3个在标记中设置的洗手间的元素。
The hiding function has worked before but when I set it up like this it doesn't seem to go. 隐藏功能以前曾起作用过,但是当我像这样设置它时,它似乎并没有消失。 Any ideas?
有任何想法吗?
function search(){
$baths = $('#baths').val();
$beds = $('#bedrooms').val();
$style = $('#modelstyle').val();
$("models").show();
$("[data-baths]").filter(function(){
return parseInt($(this).attr('data-baths'), 10) < $baths;
}).hide();
$("[data-beds]").filter(function(){
return parseInt($(this).attr('data-beds'), 10) < $beds;
}).hide();
$("[data-style]").filter(function(){
return parseInt($(this).attr('data-style'), 10) != $style;
}).hide();
};
$('#search').click(search());
You need to give the reference to the search()
function to the handler, not the result of the function. 您需要将对
search()
函数的引用提供给处理程序,而不是函数的结果 。 Remove the brackets after the search
call: search
电话后,请删除括号:
$('#search').click(search);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.