根据特定文本

时间:2015-12-28 06:10:43

标签: jquery

我使用jQuery来查找和替换单选按钮文本但是,我编写的代码删除了整个单选按钮并用文本替换它。 我在桌子上有3个单选按钮(低,中,高),我想找到带有"高"的单选按钮。并将其文本替换为" finished"等其他内容。

这是我的代码,可以更好地解释:



var tableRow = $("td").filter(function() {
  return $(this).text() == "High";
}).text("High", "Finished");

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="nf-filler-control-inner">
  <table id="ctl00_ctl40_g_c8f035da_e217_44d5_83e0_bff9b94d32fc_ctl00_ctl00_formFiller_FormView_ctl122_065ff240_02d6_4583_a9b1_948238768260" class="aspNetDisabled  nf-associated-control" data-use-attribute-as-value="data-nfChoiceValue" style="width:100%;">
    <tbody>
      <tr>
        <td><span class="aspNetDisabled" data-nfchoicevalue="Low"><input id="ctl00_ctl40_g_c8f035da_e217_44d5_83e0_bff9b94d32fc_ctl00_ctl00_formFiller_FormView_ctl122_065ff240_02d6_4583_a9b1_948238768260_0" type="radio" name="ctl00$ctl40$g_c8f035da_e217_44d5_83e0_bff9b94d32fc$ctl00$ctl00$formFiller$FormView$ctl122$065ff240_02d6_4583_a9b1_948238768260" value="Low" checked="checked" disabled="disabled" formcontrolid="1a427d12-7f9d-4c49-97b1-e751e43c36ac"><label for="ctl00_ctl40_g_c8f035da_e217_44d5_83e0_bff9b94d32fc_ctl00_ctl00_formFiller_FormView_ctl122_065ff240_02d6_4583_a9b1_948238768260_0">Low</label></span></td>
        <td><span class="aspNetDisabled" data-nfchoicevalue="Medium"><input id="ctl00_ctl40_g_c8f035da_e217_44d5_83e0_bff9b94d32fc_ctl00_ctl00_formFiller_FormView_ctl122_065ff240_02d6_4583_a9b1_948238768260_1" type="radio" name="ctl00$ctl40$g_c8f035da_e217_44d5_83e0_bff9b94d32fc$ctl00$ctl00$formFiller$FormView$ctl122$065ff240_02d6_4583_a9b1_948238768260" value="Medium" disabled="disabled"><label for="ctl00_ctl40_g_c8f035da_e217_44d5_83e0_bff9b94d32fc_ctl00_ctl00_formFiller_FormView_ctl122_065ff240_02d6_4583_a9b1_948238768260_1">Medium</label></span></td>
        <td><span class="aspNetDisabled" data-nfchoicevalue="High"><input id="ctl00_ctl40_g_c8f035da_e217_44d5_83e0_bff9b94d32fc_ctl00_ctl00_formFiller_FormView_ctl122_065ff240_02d6_4583_a9b1_948238768260_2" type="radio" name="ctl00$ctl40$g_c8f035da_e217_44d5_83e0_bff9b94d32fc$ctl00$ctl00$formFiller$FormView$ctl122$065ff240_02d6_4583_a9b1_948238768260" value="High" disabled="disabled"><label for="ctl00_ctl40_g_c8f035da_e217_44d5_83e0_bff9b94d32fc_ctl00_ctl00_formFiller_FormView_ctl122_065ff240_02d6_4583_a9b1_948238768260_2">High</label></span></td>
      </tr>
    </tbody>
  </table>
</div>
&#13;
&#13;
&#13;

代码:

 var tableRow = $("td").filter(function() {
 return $(this).text() == "Medium";
}).text("High", "Finished");

1 个答案:

答案 0 :(得分:1)

要按标签内容更改标签,您可以使用

$("td label").filter(function() {
   return $.trim($(this).text()) == "Medium";
 }).text("Finished");

Working Demo

要检查输入值并更改其标签文字,您可以使用

$("td input").filter(function() {
   return $.trim($(this).val()) == "Medium";
 }).closest('td').find('label').text("Finished");

Working Demo

  

注意:我更喜欢使用$ .trim(值/文字)来避免左右空格