开发者

Combine Events - Event Handler Passing

开发者 https://www.devze.com 2022-12-16 19:22 出处:网络
I can\'t for the life of me figure out how you would do the following: $(\"select\").change(function() {

I can't for the life of me figure out how you would do the following:

$("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('chec开发者_如何转开发ked', false) 
});

As one function. Really don't like repetition, especially when they're doing the exact same thing.

My jQuery jedi skills aren't tailored to this scenario.


You first declare the function:

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

and then add the assignments:

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

精彩评论

暂无评论...
验证码 换一张
取 消