jQuery函数隐藏基于不在Click上运行的HTML5数据标签的元素

时间:2015-01-07 15:41:23

标签: javascript jquery html5

我一直在研究jQuery中的一个小函数,以根据数据标签中的值隐藏元素。例如,如果浴室的值为3,则隐藏标签中设置的少于3个浴室的所有元素。

隐藏功能之前有效,但是当我这样设置它时似乎没有。有什么想法吗?

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());

1 个答案:

答案 0 :(得分:4)

您需要将search()函数的引用提供给处理程序,而不是函数的结果search致电后删除括号:

$('#search').click(search);