1

我在页面上有一个网格,其中包含一系列动态生成的单选按钮。所有单选按钮都有一个相似的 ID 名称,除了 ID 名称的中间 - 一个增量变量 CTL##(ctl00、ctl01 等)

<input id="ctl00_PageBody_grdCustomer_ctl01_rdoCustomerSelect" type="radio" value="rdoCustomerSelect">
<input id="ctl00_PageBody_grdCustomer_ctl02_rdoCustomerSelect" type="radio" value="rdoCustomerSelect">
<input id="ctl00_PageBody_grdCustomer_ctl03_rdoCustomerSelect" type="radio" value="rdoCustomerSelect">
<input id="ctl00_PageBody_grdCustomer_ctl04_rdoCustomerSelect" type="radio" value="rdoCustomerSelect">
<input id="ctl00_PageBody_grdCustomer_ctl05_rdoCustomerSelect" type="radio" value="rdoCustomerSelect">

如果它以特定模式开始/结束,我已经找到匹配 ID 的代码,但无法弄清楚如何提取匹配模式并将其分配给属性。

我的最终目标是使标签的格式如下...

<input id="ctl00_PageBody_grdCustomer_ctl01_rdoCustomerSelect" type="radio" value="rdoCustomerSelect" title="rdo_ctl01">
<input id="ctl00_PageBody_grdCustomer_ctl02_rdoCustomerSelect" type="radio" value="rdoCustomerSelect" title="rdo_ctl02">
<input id="ctl00_PageBody_grdCustomer_ctl03_rdoCustomerSelect" type="radio" value="rdoCustomerSelect" title="rdo_ctl03">

等等...

有什么建议么?谢谢。

4

2 回答 2

1

尝试:

$('input').attr('title', function () {
    var matches = $(this).attr('id').match(/ctl00_PageBody_grdCustomer_(.*)_rdoApplicantSelect/);
    return 'rdo_' + matches[1];
});

jsFiddle 示例

这会产生:

<input id="ctl00_PageBody_grdCustomer_ctl01_rdoApplicantSelect" type="radio" value="rdoApplicantSelect" title="rdo_ctl01">
<input id="ctl00_PageBody_grdCustomer_ctl02_rdoApplicantSelect" type="radio" value="rdoApplicantSelect" title="rdo_ctl02">
<input id="ctl00_PageBody_grdCustomer_ctl03_rdoApplicantSelect" type="radio" value="rdoApplicantSelect" title="rdo_ctl03">
<input id="ctl00_PageBody_grdCustomer_ctl04_rdoApplicantSelect" type="radio" value="rdoApplicantSelect" title="rdo_ctl04">
<input id="ctl00_PageBody_grdCustomer_ctl05_rdoApplicantSelect" type="radio" value="rdoApplicantSelect" title="rdo_ctl05">
于 2013-10-16T14:53:28.303 回答
0

你可以使用split()

$('input').attr('title', function() {
    return 'rdo_' + this.id.split('_')[3]; 
});

这是一个小提琴

于 2013-10-16T14:57:34.067 回答