js中,如何把这三个绑定写成一个?

$('#hetong_fu1').bind('input propertychange',function(){
    debugger;
    $('#deposit1').val(car_deposit());
});
$('#hetong_ya1').bind('input propertychange',function(){
    debugger;
    $('#deposit1').val(car_deposit());
});
$('.floor_rent').bind('input propertychange',function(){
    debugger;
    $('#deposit1').val(car_deposit());
});
阅读 2.5k
4 个回答
$('#hetong_fu1, #hetong_ya1, .floor_rent').bind('input propertychange',function(){
    debugger;
    $('#deposit1').val(car_deposit());
});

先为这些元素添加一样的classname,比如hetong,然后jq有隐形迭代,下面这么做就可以了。

$('.hetong').bind('input propertychange',function(){
    debugger;
    $('#deposit1').val(car_deposit());
});

你这样试试

$('#hetong_fu1,#hetong_ya1,.floor_rent').bind('input propertychange',function(){

debugger;
$('#deposit1').val(car_deposit());

});

将上面的绑定元素改成变量,比如:

function bindEvent(element){
    $(element).bind("input propertychange", function(){
        ...
    });
}
调用方式
bindEvent("#hetong_ya1");
或者:
var arr = ["#hetong_fu1", "#hetong_ya1", ".floor_rent"];
arr.forEach(function(item, index, array){
    bindEvent(item);
});
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题