3

如何删除 jquery 中的前缀。?

<td><span id="stu1" class="reject-student ">Not Selected</span></td>
<td><span id="stu2" class="select-student ">Selected</span></td>
<td><span id="stu5" class="select-student ">Selected</span></td>

jQuery:

var selected = $(".select-student").map(function() {
return this.id; 
}).get();

我试过这样:

var selected = $(".select-student").map(function() {
var id = $('span[id^="stu"]').remove();
return this.id; 
}).get();

我得到像 stu1 stu2 这样的结果,我只想发送 1 和 2 .. 我该怎么做。?

4

1 回答 1

5

您不需要$('span[id^="stu"]').remove();带有 remove 元素的语句。

一个简单的解决方法是使用stuString.prototype.replace()替换的方法

var selected = $(".select-student").map(function() {
   return this.id.replace('stu', ''); 
}).get();

此外,您还可以使用 RegEx 删除所有非数字字符

var selected = $(".select-student").map(function() {
   return this.id.replace (/[^\d]/g, ''); 
}).get();
于 2017-01-11T08:55:10.073 回答