如何在jquery id中删除前缀

Abi*_*bid 3 javascript forms jquery serialization array.prototype.map

如何在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>
Run Code Online (Sandbox Code Playgroud)

jQuery的:

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

我有这样的trid:

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

我得到的结果就像stu1 stu2我想只发送1和2 ..我怎么能这样做.

Sat*_*pal 5

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

一个简单的解决方案是使用该String.prototype.replace()方法来替换stu

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

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

var selected = $(".select-student").map(function() {
   return this.id.replace (/[^\d]/g, ''); 
}).get();
Run Code Online (Sandbox Code Playgroud)