代码之家  ›  专栏  ›  技术社区  ›  Kieran Senior

合并事件-事件处理程序传递

  •  0
  • Kieran Senior  · 技术社区  · 16 年前

    我无法为我的生活做以下事情:

    $("select").change(function() {
      var cb = $(this).parent().next().find("input[type=checkbox]");
      $(this).val().length > 0 ? cb.attr('checked', true) : cb.attr('checked', false) 
    });
    $("input[type=text]").keyup(function() {
      var cb = $(this).parent().next().find("input[type=checkbox]");
      $(this).val().length > 0 ? cb.attr('checked', true) : cb.attr('checked', false) 
    });
    

    作为一项功能。我真的不喜欢重复,尤其是当他们做完全相同的事情时。

    1 回复  |  直到 16 年前
        1
  •  2
  •   David Hedlund    16 年前

    首先声明函数:

    function myFunc() {
       var cb = ...
    }
    

    $('select').change(myFunc);
    $('input[type=text]').keyup(myFunc);