2013-08-05 33 views
1

我需要允許用戶將onlu數字字符輸入到使用不同ID的兩個文本框中。在多個ID上應用jquery函數

 $('#signup-phone-number').keyup(function() { 
      $(this).numeric(); 
     }); 

上面的代碼是我目前擁有的。現在我試圖將其更改爲:

 $('#signup-phone-number', '#mobile-number').keyup(function() { 
      $(this).numeric(); 
     }); 

上面的代碼無法正常工作。我只需要使用Id的。沒有課。

我該怎麼做?

回答

6

儘量使用像

$('#signup-phone-number , #mobile-number').keyup(function() { 
    $(this).numeric(); 
}); 

即使你可以給所有的人一個UNIQUE class,並使用您想在這個類像

$('.common_class').keyup(function() { 
    $(this).numeric(); 
}); 
1

的情況下使用相同的引號內的選擇:

$('#signup-phone-number, #mobile-number').keyup(function() { 
    $(this).numeric(); 
}); 

或者,也可以向這兩個元素添加一個類,並使用該元素廣告,因爲它是一種更爲語義的方法。

1

multiple-selector

的多個選擇使用「#signup-phone-number , #mobile-number''#signup-phone-number' , '#mobile-number'

$('#signup-phone-number , #mobile-number').keyup(function() { 
     $(this).numeric(); 
    });