最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - jQuery, same function for multiple ids - Stack Overflow

matteradmin16PV0评论

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.

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

<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>

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.

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

<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>
Share Improve this question asked Jun 22, 2012 at 6:30 lolerloler 2,5871 gold badge21 silver badges31 bronze badges
Add a comment  | 

6 Answers 6

Reset to default 30

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

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

$('[id^=d]')

Above selector will work for all elements whose ids start with d eg d1, d2, d3 and so on.

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 = '';
    }
 }    
});
$('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, id start with d.

Read about jQuery start selector

You forgot the # for d2 & d3. And also a 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('');

        }
    }
});

You forgot the hash for the other two Ids:

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

see also jQuery Multiple ID selectors

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

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far