需要在最后一个下划线之前获取ID的一部分

时间:2015-09-17 18:33:17

标签: javascript jquery html regex

现在意识到我也想要分开并获得所有角色,然后才能获得#34; chkOut"

GridView1__ctl2_chkOut

所以我想要GridView1__ctl2_

我原来的问题:

Needing to get only part of the ID everything after last underscore

LOOP回答我喜欢的另一个问题,但我不知道完整的ID名称。

<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>

3 个答案:

答案 0 :(得分:2)

试试这个:

var text = "GridView1__ctl2_chkOut";

var firstHalf = text.substring(0, text.lastIndexOf('_') + 1);
var secondHalf = text.substring(firstHalf.length, text.length);

答案 1 :(得分:1)

试试这个:

var str = 'GridView1__ctl2_chkOut';
console.log(str.substring(0,str.lastIndexOf('_')+1));

答案 2 :(得分:0)

var str = "GridView1__ctl2_chkOut"
str.split('chkOut')[0]