简体   繁体   English

在Jquery中克隆表行

[英]Cloning table row in Jquery

Running into a bit of a brick wall with jquery, I currently have the following code: 用jquery运行到一个砖墙,我目前有以下代码:

http://jsfiddle.net/uSLhb/1/ http://jsfiddle.net/uSLhb/1/

The clone method being: 克隆方法是:

$("#addMore").click(function() {
    $('.user_restriction:last').clone().insertAfter(".user_restriction:last");
    return false;
});

As you can see I have it set up so you can easily clone the row. 如您所见,我已将其设置为可以轻松克隆该行。

The problem I am facing is showing the correct select list (In the 'Field' column) in the new cloned elements, depending on what field they select from the first select (In the 'table' column) field in the row. 我面临的问题是在新克隆元素中显示正确的选择列表(在“字段”列中),具体取决于他们从行中第一个选择(在“表”列中)字段中选择的字段。

Wondering if anyone can help me find a solution, thanks! 想知道是否有人可以帮我找到解决方案,谢谢!

Working example here - I had to use jsbin as fiddle was playing up! 这里的工作示例 - 我不得不使用jsbin作为小提琴正在播放!

Clone does not maintain the selected state, so you'll need to grab the value before cloning and set it after. 克隆不会保持所选状态,因此您需要在克隆之前获取该值并在之后进行设置。

Your click now becomes: 您的点击现在变为:

$("#addMore").click(function() {
   var value = $('.user_restriction:last').find('select').val(); 
  $('.user_restriction:last').clone().insertAfter(".user_restriction:last");

        //alert(value);
$('.user_restriction:last').find('select').val(value);
    return false;
});

The problem you are facing occurs, because you are using IDs for your selects. 您遇到的问题出现了,因为您使用的ID是您的选择。 IDs must be unique in a page, otherwise you'll run into trouble. ID 必须在页面中是唯一的,否则您将遇到麻烦。 If you're cloning a select with an id, the id will be cloned too, thus producing an invalid document. 如果您正在克隆带有id的select,则也会克隆id,从而生成无效文档。

Have a look at the example I created on JsBin 看看我在JsBin上创建的示例

Markup: 标记:

<tr class="user_restriction">
    <td>
        <select name="table[]" class="userselect">
            <option value="" selected>---</option>
            <option value="members">Members</option>
            <option value="staff_positions">Staff Positions</option>
        </select>
    </td>
    <!-- and so on, basically just changed ids to classes -->            
</tr>

I changed all the IDs to classes and altered the change-handler, because this was your second problem. 我将所有ID更改为类并更改了更改处理程序,因为这是您的第二个问题。

An event-handler gets bound to the elements that are selected, not to those you add afterwards. 事件处理程序绑定到所选的元素,而不是之后添加的元素。 This is a proper use-case for event-delegation, when the handler is bound to a parent and catches, in this example, the change event from a child select-element, no matter when it was added to the DOM. 这是事件委派的正确用例,当处理程序绑定到父级并在此示例中捕获来自子select元素的change事件时,无论它何时被添加到DOM。

Using jQuery, this is a way to achieve this: 使用jQuery,这是实现此目的的一种方法:

$('table.table').on('change', '.userselect', function() {

    var activeRow = $(this).parents('tr');

    activeRow.find('td').eq(1).find('select').hide();

    if(this.value.length === 0){
        activeRow.find('td').eq(1).find('select').eq(0).show();
    }else{
        activeRow.find("." + this.value).show();
    }

});

The handler is bound to your table - element, .userselect is a class I added to the first select in a row. 处理程序绑定到您的table - 元素, .userselect是我添加到行中第一个选择的类。 So every change on an element that was added later would be handled too. 因此,稍后添加的元素的每个更改也将被处理。 In the handler, I changed the behaviour to affect only the actual table-row, not the whole table. 在处理程序中,我将行为更改为仅影响实际的表行,而不是整个表。

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

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