如何使用jquery触发器功能?

时间:2017-03-10 12:15:34

标签: jquery html

我有两个函数sidefilter,另一个是topfilter

user.js的

sidefilter();
function sidefilter(){
    $('.sidefilter').unbind("click");
    $('.sidefilter').on("click",function(e){
        var fid=$(this).attr('data-id');
        var filtertype=$(this).attr('data-type');
        var type = $('.view-list').hasClass("active")?"list":"grid";
        $.post(urljs+"user/FilterController/index",{'fid':fid, 'filtertype':filtertype, 'type':type},function(data){
            $("#sidefilter").html(data.view);
            sidefilter();
            topfilter();
        },"json");
        e.preventDefault();
    });
}

topfilter();
function topfilter(){
    $("[name=topfilter]").unbind('change'); 
    $("[name=topfilter]").on("change",function(e){


   **I want to use trigger event here and use of that i want call sidefilter function. This topfilter is drop down input**

        },"json");   
    });
}

courses.php

 <select class="select" data-type="topfilter" name="topfilter" id="all-categories">
    <option value="">All</option>
    <option value="1">Beginer</option>
    <option value="2">Intermediate</option>
    <option value="3">Professional</option>
 </select>

1 个答案:

答案 0 :(得分:2)

以下是如何使用trigger()函数的示例:See this fiddle

<强> JS

$('.sidefilter').click(function(){
    alert('triggered!');
});

$("[name=topfilter]").on("change",function(e){
  $('.sidefilter').trigger('click');
});

希望它有所帮助!

文档: http://api.jquery.com/trigger/