简体   繁体   English

未能触发onChange事件

[英]Failing to trigger the onChange event

I've got this piece of code, to search the contents of HTML elements with the class "widget" and use the jQuery functions fadeIn() and fadeOut() to only show elements that match the search term. 我有这段代码,可以使用“ widget”类搜索HTML元素的内容,并使用jQuery函数fadeIn()fadeOut()来仅显示与搜索词匹配的元素。 I don't know how to assign the search function to, because I don't assign it to the search box's change event. 我不知道如何分配搜索功能,因为我没有将其分配给搜索框的change事件。 What is the equal of onchange= in jQuery? jQuery中的onchange=等于什么? My current code: 我当前的代码:

function search(text, elements) {
    var term = text.value.toLowerCase();
    var elms = $(elements);
    var elm;
    for (var i = elms.length; i > 0; i-=1){
        elm = elms.get(i);
        if (elm.text().toLowerCase().indexOf(term)>=0 ) {
            elm.fadeOut(500);
        } else {
            elm.fadeIn(500);
        }

    }
}

And the failing inline script to trigger the onchange: 失败的内联脚本触发onchange:

<script type="text/javascript">
    $(document).ready(function() {
    $("#search").change(function() {
            search($("#search").text(),".widget");
        });
    });

我会使用keyup()而不是change,Change仅在模糊时触发。

$("#search").change(function() { search($("#search").text(),".widget"); }); $(“#search”)。change(function(){search($(“#search”)。text(),“。widget”);});

.change() is a reference to the .on() function in JQuery, so if you would like you could rewrite it this way : .change()是对JQuery中的.on()函数的引用,因此,如果您希望以这种方式重写它:

$("#search").on('change' , function(){ search($(this).val(),".widget"); }); $(“#search”)。on('change',function(){search($(this).val(),“。widget”);});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM