jQuery功能,用于隐藏基于未在点击上运行的HTML5数据标签的元素

jQuery function to hide elements based on HTML5 data tags not running on Click

本文关键字:运行 HTML5 数据 元素 标签 用于 功能 隐藏 于未 jQuery      更新时间:2023-09-26

我一直在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());
您需要

向处理程序提供对search()函数的引用,而不是函数的结果。在search呼叫后取下括号:

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