【问题标题】:JQuery identifying element id through variable: multiple table rowsJQuery通过变量识别元素id:多表行
【发布时间】:2016-04-16 20:02:16
【问题描述】:

我在每个标签中都有表格,如http://jsfiddle.net/Us8uc/5020/所示

每个表中的行必须在按钮单击时添加和删除。

我正在为每个具有不同表的表复制行添加函数 $("#anc_add2").click(function() {} 和行删除函数 $("#anc_rem2").click(function(){ id 例如 tabl1、tabl2、tabl3 等。

以下函数对于添加和删除表中的行很重要:

 $('#tbl2 tr').last().after(rowdata);
 $('#tbl2 tr:last-child').remove();

如何将TAB编号作为参数传递给函数,以便可以使用变量生成表id并且可以使用该变量;像这样:

function onBtnClick(tabnumber) {
var tableid = "tabl" + tabnumber;
$(tableid tr).last().after(rowdata);
$(tableid tr:last-child').remove();
}

示例代码可在http://jsfiddle.net/Us8uc/5020/获取

【问题讨论】:

  • 您正在创建具有相同 ID 的元素。这是错误的。

标签: javascript jquery


【解决方案1】:

使用data-* 属性来维护自定义数据。

要在其他方法中使用selected-tab-index,请将其用作global-variable

另请注意,在处理动态元素时,请使用classes 而不是id 属性。

$(document).ready(function() {
  var global = 1;
  $(".tabs-menu a").click(function(event) {
    global = $(this).data('id');
    event.preventDefault();
    $(this).parent().addClass("current");
    $(this).parent().siblings().removeClass("current");
    var tab = $(this).attr("href");
    $(".tab-content").not(tab).css("display", "none");
    $(tab).fadeIn();
  });
  $(".anc_add").click(function() {
    var rowdata = '<tr > <td> <input size="5" type="text">	</td>';
    rowdata += '<td> <input  size="5"  type="text">	</td>	</tr>';
    $('#tbl' + global + ' tr').last().after(rowdata);
  });

  $(".anc_rem").click(function() {
    if ($('#tbl' + global + ' tr').size() > 1) {
      $('#tbl' + global + ' tr:last-child').remove();
    } else {
      alert('One row should be present in table');
    }
  });
});
body {
  padding: 20px;
  font-family: Arial, Helvetica, sans-serif;
  line-height: 1.5;
  font-size: 14px;
}
.tabs-menu {
  height: 30px;
  float: left;
  clear: both;
}
.tabs-menu li {
  height: 30px;
  line-height: 30px;
  float: left;
  margin-right: 10px;
  background-color: #ccc;
  border-top: 1px solid #d4d4d1;
  border-right: 1px solid #d4d4d1;
  border-left: 1px solid #d4d4d1;
}
.tabs-menu li.current {
  position: relative;
  background-color: #fff;
  border-bottom: 1px solid #fff;
  z-index: 5;
}
.tabs-menu li a {
  padding: 10px;
  text-transform: uppercase;
  color: #fff;
  text-decoration: none;
}
.tabs-menu .current a {
  color: #2e7da3;
}
.tab {
  border: 1px solid #d4d4d1;
  background-color: #fff;
  float: left;
  margin-bottom: 20px;
  width: auto;
}
.tab-content {
  width: 660px;
  padding: 20px;
  display: none;
}
#tab-1 {
  display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div id="tabs-container">
  <ul class="tabs-menu">
    <li class="current"><a href="#tab-1" data-id='1'>Tab 1</a>
    </li>
    <li><a href="#tab-2" data-id='2'>Tab 2</a>
    </li>
    <li><a href="#tab-3" data-id='3'>Tab 3</a>
    </li>
    <li><a href="#tab-4" data-id='4'>Tab 4</a>
    </li>
  </ul>
  <div class="tab">
    <div id="tab-1" class="tab-content">

      <!-- Add row and Delete buttons -->
      <a href="javascript:void(0);" class='anc_add'>Add Row</a>
      <a href="javascript:void(0);" class='anc_rem'>Remove Row</a>

      <!-- Table 1 in TAB 1 -->

      <table class="table text-center table-bordered table-striped volumes tabcenter" style="align:center; margin:15px; width:40%" border="1">
        <thead>
          <tr>
            <th><b>Node</b> 
            </th>
            <th><b>Data</b> 
            </th>
          </tr>
        </thead>
        <tbody id="tbl1">

          <tr>
            <td>
              <input size="5" type="text" id="Node_TR11">
            </td>
            <td>
              <input size="5" type="text" id="Data_TR11">
            </td>
          </tr>
        </tbody>
      </table>

    </div>
    <!-- End of Tab 1 -->
    <!-- Tab 2 Start -->
    <div id="tab-2" class="tab-content">
      <!-- Add row and Delete buttons for TAB2 -->
      <a href="javascript:void(0);" class='anc_add'>Add Row</a>
      <a href="javascript:void(0);" class='anc_rem'>Remove Row</a>
      <!-- Table 2 in TAB 2 -->

      <table class="table text-center table-bordered table-striped volumes tabcenter" style="align:center; margin:15px; width:40%" border="1">
        <thead>
          <tr>
            <th><b>Node</b> 
            </th>
            <th><b>Data</b> 
            </th>
          </tr>
        </thead>
        <tbody id="tbl2">

          <tr>
            <td>
              <input size="5" type="text" id="Node_TR21">
            </td>
            <td>
              <input size="5" type="text" id="Data_TR21">
            </td>
          </tr>
        </tbody>
      </table>



    </div>

    <div id="tab-3" class="tab-content">
      <p>TAB 3: Hello World !</p>
    </div>
    <div id="tab-4" class="tab-content">
      <p>TAB 4: Hello World!</p>
    </div>
  </div>
</div>

$(this).data('id') 可用于访问data-id

Fiddle here

【讨论】:

  • 我想知道如何在这里使用变量作为元素id $('#tbl2 tr').last().after(rowdata);如何将#tabl2 或#tabl1 替换为变量。我尝试使用 var tableid = "tabl" + id.toString();并用作 $(tableid tr).last().after(rowdata);它不工作!
  • 首先,ID 必须是唯一的。您需要使用classes,因为使用id 选择器附加的事件将附加到具有指定ID 的第一个元素。使用global变量并将所选选项卡的值存储在全局变量中。现在您可以在其他功能中使用它...试试这个!
  • 试过 $('#tbl+tabnum tr').last().after(rowdata);不工作!它没有调用该函数。两个具有相同 id='anc_add 的“添加行”链接。如果我在一个 TAB 中只使用一个“添加行”jsfiddle.net/Us8uc/5036,它就可以工作
  • 你检查过我提供的小提琴吗?您仍在处理id 属性.. 使用classes.. 还要查看我更新的答案...
  • 刚刚看了下,我正在找这个!! $('#tbl' + 全局 + 'tr').last().after(rowdata);双引号,单引号在 concat 时让我感到困惑!谢谢。
【解决方案2】:

你需要这个 html 而不是你的:

<a href="javascript:void(0);" class="anc_add">Add Row</a>
<a href="javascript:void(0);" class="anc_rem">Remove Row</a>

永远不要在您的 html 中重复 ids,因为它会使您的 html 以低于标准的方式工作。然后你需要这样的函数:

function addRow(context) {
    //add a row using the context object
}

function remRow(context) {
    //remove a row using the context object
}

function getContext(referrer) {
    return referrer.find("table");
}

$(function() {
    $(".anc_add").click(function () {
        addRow(getContext($(this).parent()));
    });

    $(.anc_rem).click(function() {
        remRow(getContext($(this).parent()));
    });
});

顺便说一句,如果你不能改变你的结构并且你有多个ids 的值是anc_add,那么你可以用属性选择器来选择它们:

$("[id=anc_add]")

作为

$("#anc_add")

将在找到第一个元素时停止,因为id 被认为是唯一的。

【讨论】:

    猜你喜欢
    • 2022-01-23
    • 1970-01-01
    • 2019-08-27
    • 2021-02-16
    • 2016-05-09
    • 1970-01-01
    • 2021-11-15
    • 2011-02-23
    • 2023-04-07
    相关资源
    最近更新 更多