2013-12-12 26 views
1

我期待采取如下:这些组合成一个功能 - 即做同样的事情 - jQuery的

$('.formWrapper').on('change', 'select', function(){ 
     data[$(this).attr('name')] = $(this).val(); 
     console.log(data); 
    }); 

    $('.formWrapper').on('change', 'input', function(){ 
     data[$(this).attr('name')] = $(this).val(); 
    }); 

并将其转换成一个功能它都手表和input ... select会是什么最简单的方法来做到这一点?

回答

7
$('.formWrapper').on('change', 'select , input', function(){ 
    data[$(this).attr('name')] = $(this).val(); 
    console.log(data); 
}); 
+0

我不知道你可以做“ATTR,ATTR,...” – TheWebs

+0

@TheWebs:逗号,您可以将多个选择到一个:http://api.jquery.com/multiple-selector/ –

+0

你可以将它们加上逗号分隔,所有你的选择器 – Bhadra

相关问题