简体   繁体   English

动态表行+链式选择

[英]Dynamic Table Rows + Chained Select

I need the ability to dynamically add and remove an indefinite number of table rows. 我需要能够动态添加和删除无限数量的表行的功能。 I also need the ability to chain select boxes within those table rows. 我还需要能够在这些表行中链接选择框。 I get the expected result (which is wrong), but I'm not a Javascript buff and I have no idea what I'm doing. 我得到了预期的结果(这是错误的),但是我不是Java爱好者,所以我不知道自己在做什么。

I'm using the chained.js script from Mika Tuupola and the "dynamic table" script found pretty much anywhere. 我正在使用Mika Tuupolachained.js脚本,并且几乎在任何地方都可以找到“动态表”脚本。

<table id="table">
<tr><td><input id="dept" name="dept[]" /></td>
    <td><input id="service" name="service[]" /></td>
    <td><input id="reason" name="reason[]" /></td>
</tr>
</table>

<script language="javascript">
     $("#service").chained("#dept");
     $("#reason").chained("#dept");

     function addRow(table) {
          var row = $('#table tbody>tr:last').clone(false);
          row.insertAfter('#table tbody>tr:last');
     }

     function delRow(el) {

          while (el.parentNode && el.tagName.toLowerCase() != 'tr') {
               el = el.parentNode;
      }

          if (el.parentNode && el.parentNode.rows.length > 2) {
               el.parentNode.removeChild(el);
          }
     }
</script>

Hopefully that's enough to get the point across. 希望这足以说明要点。

The obvious happens, since the chained select is based on ID. 很明显发生了,因为链接的选择基于ID。 Once you make selections in the first drop down, the "child" drop downs are chained to that first one. 在第一个下拉列表中进行选择后,“子”下拉列表将链接到第一个下拉列表。

My suspicion is that I need to somehow modify the ID for each of those select boxes and then increment the IDs in the chaining assignments. 我的怀疑是,我需要以某种方式修改每个选择框的ID,然后在链接分配中增加ID。 But I also suspect that there's probably an easier way to do it or that it's going to require some kind of delegation, since I might need to destroy and reassign the chain after adding the table row...? 但是我还怀疑这样做可能更简单,或者需要某种委派,因为添加表行之后我可能需要销毁并重新分配链...?

I have no idea. 我不知道。 Any pointer in the right direction would be greatly appreciated. 朝正确方向的任何指针将不胜感激。

UPDATE UPDATE

I made a fiddle. 我做了一个小提琴。 It only works in Chrome. 它仅适用于Chrome。 Some combinations of frameworks/extensions work and partially work in other browsers. 框架/扩展的某些组合在其他浏览器中有效且部分有效。 But the only one I could make the example work in was Chrome. 但是我唯一可以使该示例运行的是Chrome。 Sorry, I don't have a lot of practice with JSFiddle. 抱歉,我在JSFiddle中没有太多练习。

http://jsfiddle.net/J2Vef/11/ http://jsfiddle.net/J2Vef/11/

Note what happens if you select an option from the first dropdown, then add a row. 请注意,如果您从第一个下拉列表中选择一个选项,然后添加一行,将会发生什么。 All options in the second drop down for added rows will be the options for the first dropdown in the first row. 添加行的第二个下拉菜单中的所有选项将是第一行中第一个下拉菜单的选项。

If you add a row and then start trying to select options, no matter what you put in the first drop down, the second drop down never becomes active. 如果您添加一行,然后开始尝试选择选项,则无论您在第一个下拉菜单中输入了什么内容,第二个下拉列表都不会处于活动状态。

Give something like this a try. 试试这个吧。 The problem you are running up against is that you can only have one element with one id. 您遇到的问题是只能有一个具有一个id的元素。 In the example you provided you were cloning the table row element but not changing the id name for the selects. 在您提供的示例中,您正在克隆表行元素,但未更改选择的ID名称。 In additional you would also need to chain the new selects together by their new id's. 另外,您还需要通过新ID将新选择链接在一起。

Here is the jsFiddle 这是jsFiddle

I am sure that they may be a cleaner way to write this code block but it will help get you going again. 我相信它们可能是编写此代码块的一种更干净的方法,但是它将帮助您重新开始。

 var row = $('#tableID tbody:first').html();
 $("#series").chained("#mark");

 function addRow(tableID) {
      $(row).appendTo('#tableID');
      var rowCnt = $('#tableID tbody>tr').length;

     $('#tableID tbody>tr:last select:first').attr('id','mark'+rowCnt);
     $('#tableID tbody>tr:last select:last').attr('id','series'+rowCnt);

    $('#series'+rowCnt).chained("#mark"+rowCnt);
  }

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

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