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

javascript - Jquery - check to see if selector ends with number - Stack Overflow

programmeradmin4浏览0评论

How can I check selector to see if he is ending with number? JS code:

if(!$(this).val())
{
   $('label[for^=image], input[id^=image], input[name=time]').remove();                              
}

I tried adding /d/, but it does not work (

$('label[for^=image' + /d/ +'], ....

)

How can I check selector to see if he is ending with number? JS code:

if(!$(this).val())
{
   $('label[for^=image], input[id^=image], input[name=time]').remove();                              
}

I tried adding /d/, but it does not work (

$('label[for^=image' + /d/ +'], ....

)

Share Improve this question asked Jun 18, 2012 at 12:15 SashaSasha 8,72524 gold badges98 silver badges181 bronze badges 1
  • So actually you want to test whether the value of the for attribute ends with a number? The whole string 'label[for^=image], input[id^=image], input[name=time]' is the selector... – Felix Kling Commented Jun 18, 2012 at 12:22
Add a ment  | 

4 Answers 4

Reset to default 7

demo is this what you are looking for: http://jsfiddle/8g2WL/

Behaviour: for the id's ending up with number in label you will see an alert.

Hope this helps, please let me know if I missed anything.

sample code using regex

$('label').each(function(){

    if ($(this).attr("id").match(/\d+$/) != null)
       alert($(this).attr("id").match(/\d+$/));


    });​

html

<label id="foo">hulk</label><br/>

<label id="foo_23">HUlk with number</label><br/>


<label id="foo1">ironman with number </label><br/>

<label id="foo">hulk</label><br/>
​

you may need to use .filter() to match with regex:

$('label').filter(function(){
    var forValue = $(this).attr("for") || '';
    return /^image\d+/.test(forValue);
});
$('label')
    .filter(function() {
        return $(this).attr("for").search(/image\d/)==0;
    })
    .html("Matched!")
    ;

Use extension Regex Selector for jQuery. Preview - http://jsfiddle/yDA9n/

$('label:regex(id,[0-9]$)').css('color', 'red');​​​​​​
发布评论

评论列表(0)

  1. 暂无评论