Jquery - 检查选择器是否以数字结尾



如何检查选择器以查看他是否以数字结尾?JS代码:

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

我尝试添加/d/,但它不起作用(

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

演示是您要查找的:http://jsfiddle.net/8g2WL/

行为:对于 ID 以数字结尾label您将看到警报。

希望这有帮助,如果我错过了什么,请告诉我。

使用正则表达式的示例代码

$('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/>
​

您可能需要使用 .filter() 来匹配正则表达式:

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

使用扩展 Regex Selector for jQuery。预览 - http://jsfiddle.net/yDA9n/

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

最新更新