根据另一列中的字符串隐藏表列中的按钮

时间:2017-04-24 18:00:39

标签: jquery

仅当class="center outstanding"包含文字.small时,才需要使用XYZ删除td中的表单按钮。我正在使用下面的代码,但它只删除第一个实例并停止。非常感谢任何帮助!

if ($("div.subtle:contains('XYZ')").length) {
  $('#pay').hide();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="accounts" class="billing">
  <tbody>
    <tr title="Details">
      <td class="tblLeft">
        <div class="small">ABC Company</div>
      </td>
      <td class="center outstanding">$0.00</td>
    </tr>
    <tr title="Details">
      <td class="tblLeft">
        <div class="small">XYZ Company</div>
      </td>
      <td class="center outstanding">$161.47<br />
        <form autocomplete="off" method="post" action="/paythebill.asp" id="payaccountform"><input class="button completeworkflow" type="submit" name="pay" id="pay" value="Pay" title="" /></form>
      </td>
    </tr>
    <tr title="Details">
      <td class="tblLeft">
        <div class="small">XYZ Company</div><br /></td>
      <td class="center outstanding">$0.00<br /></td>
    </tr>
    <tr title="Details">
      <td class="tblLeft">
        <div class="small">ABC Company</div>
      </td>
      <td class="center outstanding">$122.88<br />
        <form autocomplete="off" method="post" action="/paythebill.asp" id="payaccountform"><input class="button completeworkflow" type="submit" name="pay" id="pay" value="Pay" title="" /></form>
      </td>
    </tr>
  </tbody>
</table>

1 个答案:

答案 0 :(得分:1)

要实现此目的,您可以使用:contains查找相关的.small元素,然后遍历DOM以查找同一form和{{}内的相关tr元素1}}它。试试这个:

hide()
$('.small:contains("XYZ")').closest('tr').find('form, br').hide()

另请注意,您在HTML中重复了<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table id="accounts" class="billing"> <tbody> <tr title="Details"> <td class="tblLeft"> <div class="small">ABC Company</div> </td> <td class="center outstanding">$0.00</td> </tr> <tr title="Details"> <td class="tblLeft"> <div class="small">XYZ Company</div> </td> <td class="center outstanding">$161.47<br /> <form autocomplete="off" method="post" action="/paythebill.asp" id="payaccountform"><input class="button completeworkflow" type="submit" name="pay" id="pay" value="Pay" title="" /></form> </td> </tr> <tr title="Details"> <td class="tblLeft"> <div class="small">XYZ Company</div><br /></td> <td class="center outstanding">$0.00<br /></td> </tr> <tr title="Details"> <td class="tblLeft"> <div class="small">ABC Company</div> </td> <td class="center outstanding">$122.88<br /> <form autocomplete="off" method="post" action="/paythebill.asp" id="payaccountform"><input class="button completeworkflow" type="submit" name="pay" id="pay" value="Pay" title="" /></form> </td> </tr> </tbody> </table>pay payaccountform个属性。 id必须是唯一的。我建议将它们更改为类或在不需要时删除它们。