在多个ID上应用jquery函数

问题描述 投票:1回答:4

我需要允许用户将数字字符输入到两个使用不同ID的文本框中。

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

上面的代码是我目前拥有的。现在我试图将其更改为:

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

上面的代码不起作用。我只需要使用ID。没有课程。

我该怎么做?

jquery
4个回答
8
投票

尝试使用类似

$('#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();
    });

0
投票
  <script>
      $(function() {
        $('#segement1,#segement2,#segement3').hide()
      });
  </script>

    <div id="segement1"></div>
    <div id="segement2"></div>
    <div id="segement3"></div>
© www.soinside.com 2019 - 2024. All rights reserved.