Javascript jQuery,多个 id 的相同功能

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11151157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 04:41:31  来源:igfitidea点击:

jQuery, same function for multiple ids

javascriptjquery

提问by F0G

i want to clear specified input if the value is not number. function works for one ID, but i want it to work for multiple. of course i can write function multiple times but i dont want to do that.

如果值不是数字,我想清除指定的输入。函数适用于一个 ID,但我希望它适用于多个。当然,我可以多次编写函数,但我不想那样做。

the following code gets effect only for input with id "d". i dont know how to identify other ids. can anyone help?

以下代码仅对 id 为“d”的输入有效。我不知道如何识别其他 ID。有人可以帮忙吗?

<input id="d" />

<input id="d2" />

<input id="d3" />

<script type="text/javascript">

$('#d,d2,d3').keyup(function(){

if($('#d,d2,d3').val() != "") {

    var value = $('#d,d2,d3').val().replace(/^\s\s*/, '').replace(/\s\s*$/, '');

    var intRegex = /^\d+$/;

    if(intRegex.test(value)) {}

    else {


    $(this).val('');

    }



}

});

</script>

回答by slash197

Instead of $('#d,d2,d3')use $('#d, #d2, #d3')and for the if statement use $(this).val()

而不是$('#d,d2,d3')使用$('#d, #d2, #d3')和 if 语句使用$(this).val()

回答by Sarfraz

You can use starts withselector instead of putting in multiple ids like this:

您可以使用带有选择器的开头,而不是像这样放入多个 id:

$('[id^=d]')

Above selector will work for all elements whose ids start with deg d1, d2, d3and so on.

上面的选择器适用于所有 id 以deg d1d2d3等开头的元素。

Here is how your code should be (fixing other errors as well):

这是您的代码应该如何(也修复其他错误):

$('[id^=d]').keyup(function(){   
 if(this.value != "") {
    var value = this.value.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
    var intRegex = /^\d+$/;
    if(intRegex.test(value)) {}
    else {
      this.value = '';
    }
 }    
});

回答by thecodeparadox

$('input[id^=d]').keyup(function() {
    var val = $.trim( this.value ); // or $.trim( $(this).val() )
    if (val != "") {
        var value = val.replace(/^\s\s*/, '').replace(/\s\s*$/, ''),
            intRegex = /^\d+$/;
            if (intRegex.test(value)) {
                // do something
            } else {
                $(this).val('');
            }
    }
});?

[id^=d]is a start with selector that means, idstart with d.

[id^=d]是以选择器开头的,意思是idd.

Read about jQuery start selector

阅读jQuery 开始选择器

回答by thecodeparadox

You forgot the #for d2 & d3. And also a this.

你忘记了#for d2 & d3。还有一个this

$('#d,#d2,#d3').keyup(function(){

    if($(this).val() != "") {

        var value = $(this).val().replace(/^\s\s*/, '').replace(/\s\s*$/, '');

        var intRegex = /^\d+$/;

        if(intRegex.test(value)) {}
        else {
            $(this).val('');

        }
    }
});

回答by swapy

You can add class attribute

您可以添加类属性

   <input id="d" class="A"/> 
    <input id="d2" class="A"/> 
    <input id="d3" class="A"/> 

use following selector by class name

按类名使用以下选择器

$('.A').keyup

回答by mamoo

You forgot the hash for the other two Ids:

您忘记了其他两个 ID 的哈希值:

$('#d,#d2,#d3')

see also jQuery Multiple ID selectors

另请参阅 jQuery 多 ID 选择器