在最后一个下划线之后只需要获得部分ID [英] Needing to get only part of the ID everything after last underscore

查看:137
本文介绍了在最后一个下划线之后只需要获得部分ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望高效地获得最后一个下划线之后的所有内容,或者包括chk之后的所有内容(例如

)。

  GridView1__ctl2_chkOut  - >我只想要chkOut 

或者

  GridView1__ctl2_chkYes2  - >我只想要chkYes2 


  GridView1__ctl2_chkNo2  - >我只想要chkNo2 

GridView1__ctl45_chkNo2



我知道子字符串可能不是处理它的最好方式,除非它与其他字符一起使用,直到找到最后一个下划线或可能找到chk部分等等。不确定
var res = str.substring(1,4);
$ b HTML p>

 < td> 
< input id =GridView1__ctl2_chkOuttype =checkboxname =GridView1:_ctl2:chkOutchecked =checkedclass =out>
< / td>
< td>
< input id =GridView1__ctl2_chkYes2type =checkboxname =GridView1:_ctl2:chkYes2checked =checkedclass =yesno>
< / td>
< td>
< input id =GridView1__ctl2_chkNo2type =checkboxname =GridView1:_ctl2:chkNo2class =yesno>
< / td>


解决方案

使用 split() 并访问array



 for  

I want to efficiently get everything after the last underscore or perhaps everything after and including "chk"

Example is

GridView1__ctl2_chkOut  --> I want ONLY  chkOut

Or

GridView1__ctl2_chkYes2  --> I want ONLY  chkYes2

Or

GridView1__ctl2_chkNo2  -->  I want ONLY chkNo2

I will always know the ID e.g. GridView1__ctl45_chkNo2

I know that a substring is probably not the best way to handle it unless it was used along with something else that counted characters till finding last underscore or perhaps it found the "chk" part etc.. not sure var res = str.substring(1, 4);

HTML

   <td>
        <input id="GridView1__ctl2_chkOut" type="checkbox" name="GridView1:_ctl2:chkOut" checked="checked" class="out">
    </td>
    <td>
        <input id="GridView1__ctl2_chkYes2" type="checkbox" name="GridView1:_ctl2:chkYes2" checked="checked" class="yesno">
    </td>
    <td>
        <input id="GridView1__ctl2_chkNo2" type="checkbox" name="GridView1:_ctl2:chkNo2" class="yesno">
    </td>

解决方案

Use split() and access the last item in the array

var ids = ['GridView1__ctl2_chkOut'
  , 'GridView1__ctl2_chkYes2'
  , 'GridView1__ctl2_chkNo2'
];

for (var i = 0; i < ids.length; i++) {
  var splat = ids[i].split('_');
  console.log(splat[splat.length - 1]);
}

这篇关于在最后一个下划线之后只需要获得部分ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆