在多个元素上绑定(bind)事件的最佳方法是什么。 我写这段代码就是为了做到这一点,有什么好的方法可以做到这一点。
$('#GiftPurchase_recipient_first_name,#GiftPurchase_recipient_last_name').bind('blur', function() {
gift_to = $('#GiftPurchase_recipient_first_name').val()+" "+$('#GiftPurchase_recipient_last_name').val();
$("#gift_to").val(gift_to);
});
我有两个带有名字和姓氏的输入框,我不会将名字和姓氏显示到另一个元素中
最佳答案
为元素指定一个类并将事件添加到该类中。
$('.theclassnamethatalltheelementsshare').bind('blur', function() {
gift_to = $('#GiftPurchase_recipient_first_name').val()+""+$('#GiftPurchase_recipient_last_name').val();
$("#gift_to").val(gift_to);
});
关于javascript - 在元素上绑定(bind)事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24926761/