【问题标题】:How to remove prefix in jquery id如何删除jquery id中的前缀
【发布时间】:2017-05-25 23:30:43
【问题描述】:

如何在 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.. 我该怎么做?

【问题讨论】:

    标签: javascript jquery forms serialization array.prototype.map


    【解决方案1】:

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

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

    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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-26
      • 1970-01-01
      • 2015-10-25
      • 2013-06-10
      • 1970-01-01
      • 2021-07-08
      • 1970-01-01
      • 2012-01-31
      相关资源
      最近更新 更多