最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin13浏览0评论

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论