简体   繁体   English

如果选中复选框,如何显示每个表格行的div?

[英]How to show div per table row if checkbox is ticked?

I am trying to figure out the best way to make the currently hidden divs of each line that is generated to only show if the box is ticked (these are 2nd party name, 2nd party surname and mobile number) 我试图找出最好的方法来生成每行生成的当前隐藏的div只显示框是否勾选(这些是第二方名称,第二方姓氏和手机号码)

What I've tried: 我尝试过的:

I've tried initially having them split into seperate rows but I couldn't get that to work as trying to bind the box check to a unique ID is proving difficult because they are auto generated. 我已经尝试过将它们拆分成单独的行,但是我无法让它工作,因为尝试将盒子检查绑定到唯一的ID证明是困难的,因为它们是自动生成的。

I've also tried 我也试过了

if ($('input.checkboxAC').prop('checked')) {
          removeAttr( 'hidetopline' );

(hidetopline is the name of the div but I'm trying to figure how to do that individually, i thought removing the style of display:none could work) (hidetopline是div的名称,但我正试图弄清楚如何单独进行,我想删除显示风格:没有可行)

What I'm using: bootstrap, jquery 我正在使用的是:bootstrap,jquery

Jquery jQuery的

  function displayResult()
  {
      document.getElementById("tableAc").insertRow(-1).innerHTML = '<td class="style1" style="border-color: #FFFFFF"><input type="text" style="width: 150px"/><div class="divhidden" style="display:none"><i><br />2nd Party First Name<i/><br /><input type="text" style="width: 150px" /></div></td><td class="style1" style="border-color: #FFFFFF"><input type="text" style="width: 150px"/><div class="divhidden" style="display:none"><i><br />2nd Party Surname<i/><br /><input type="text" style="width: 150px" /></div></td><td class="style1" style="border-color: #FFFFFF"><input type="text" style="width: 150px"/><div class="divhidden" style="display:none"><i><br />Mobile<i/><br /><input type="text" style="width: 150px" /></div><td class="style1" style="border-color: #FFFFFF"><input type="checkbox" style="width: 150px"/></td></td>';

  }

HTML HTML

<h2>COA Table</h2>
  <table class="table" id="tableAc">
    <thead>
      <tr>
        <th class="style2">Product Type</th>
        <th class="style2">NSC</th>
        <th class="style2">Account Number</th>
        <th class="style2">Joint A/c</th>
      </tr>
    </thead>
    <tbody>
      <tr id="acRow" class="first">
        <td class="style1" style="border-color: #FFFFFF"><input type="text" style="width: 150px"/><div class="divhidden" ><i><br />2nd Party First Name<i/><br /><input type="text" style="width: 150px" /></div></td>
        <td class="style1" style="border-color: #FFFFFF"><input type="text" style="width: 150px"/><div class="divhidden" ><i><br />2nd Party First Name<i/><br /><input type="text" style="width: 150px" /></div></td>
        <td class="style1" style="border-color: #FFFFFF"><input type="text" style="width: 150px"/><div class="divhidden" ><i><br />2nd Party First Name<i/><br /><input type="text" style="width: 150px" /></div></td>
        <td class="style1" style="border-color: #FFFFFF"><input type="checkbox" style="width: 150px" class="checkboxAC"/></td>

      </tr>
    </tbody>
  </table>
          <button type="button" onclick="displayResult()">Add A/c</button>            

</div>

</body>
</html>

When you say you tried: 当你说你尝试过:

if ($('input.checkboxAC').prop('checked')) {
    removeAttr( 'hidetopline' );

You could try replace that with: 您可以尝试将其替换为:

if ($('input.checkboxAC').prop('checked')) {
    $('#hidetopline')attr( 'display', 'block' );
} else {
    $('#hidetopline')attr( 'display', 'none' );
}
var checkboxfunction = function(){
    var $this = $(this); // this is the checkbox
    var $row = $this.closest("tr") //this is the row
    var $fields = $row.find(".divhidden") // these are the fields
    //if you can't add a class, just build a jQuery collection manually 
    //with the required fields

    //For debug purposes:
    //console.log("I've clicked the checkbox ", $this, " for the row: ", $row, " to ", $this.is(':checked')?"show":"hide", " these fields: ", $fields);

    if ($this.is(':checked')) {
      $fields.show();
    }else{
      $fields.hide();
    }
}

the handler would ideally be bound like this: 理想情况下,处理程序将被绑定为:

$("#tableAc").on("click", "input.checkboxAC", checkboxfunction);

I binded the function to the container to not bind a function for EVERY checkbox. 我将函数绑定到容器,以便不为每个复选框绑定一个函数。 This also works with dinamically generated checkboxes inside the container. 这也适用于容器内的dinamically生成的复选框。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM