简体   繁体   English

带有隐藏行的克隆表

[英]Cloning table with hidden row

I have a repeating table with a hidden row and when clicking the a checkbox I have the row appearing, however when adding more than one table the same row always appears instead of the row that has just been cloned. 我有一个重复表,其中有一个隐藏行,当单击一个复选框时,我会出现该行,但是当添加多个表时,总会出现同一行,而不是刚刚克隆的行。 I would appreciate any help with this. 我将不胜感激。

My code looks like the following: 我的代码如下所示:

HTML: HTML:

<table class="repeatingTable">
    <tr>
        <td>
            <label for="name">Name</label>
        </td>
        <td>
            <input type="text" name="name" id="InputName" class="InputID_1" />
        </td>
        <td>
            <label for="req">Required</label>
        </td>
        <td>
            <input type="checkbox" name="req" id="CheckBox" class="ChexkBox_1" readonly="readonly" />
        </td>
    </tr>
    <tr id="HiddenFields" class="HiddenFields_1">
        <td>
            <label for="Box">Box Number</label>
        </td>
        <td>
            <input type="number" name="Box" id="InputBoxNo" class="InputBoxNo_1" readonly="readonly" />
        </td>
        <td>
            <label for="id">ID Number</label>
        </td>
        <td>
            <input type="number" name="id" id="inputNo" class="InputNo_1" />
        </td>
    </tr>
</table>
<div class="expensesBtns">
    <input id="repeatingBtn" type="button" value="Add Another" />
</div>

Javascript: Javascript:

document.getElementById("HiddenFields").style.visibility = "hidden";

 $('.ChexkBox_1').click(function () {
    if (!$(this).is(':checked')) {
        document.getElementById("HiddenFields").style.visibility = "hidden";
    }
    else {
        document.getElementById("HiddenFields").style.visibility = "visible";
    }
})


 $('#repeatingBtn').click(function (e) {
    //$('.expensesSection').clone(false).find("*[id]").andSelf().each(function () {
    //    $(this).attr("id", $(this).attr("id") + "_cloned");
    //})
    e.preventDefault();
    var lastRepeatingGroup = $('.repeatingTable').last();
    var cloned = lastRepeatingGroup.clone(true);
    cloned.insertAfter(lastRepeatingGroup);
    cloned.find("input").val("");
    //resetAttributeNames(cloned);
});

I have a js fiddle here: jsfiddle 我在这里有一个js小提琴: jsfiddle

Any help is greatly appreciated. 任何帮助是极大的赞赏。

Check your UPDATED FIDDLE . 检查您的更新字段

Worked after some changes in ChexkBox_1 click event, you have to use $(this) instead of document.getElementById("HiddenFields") to deal with current checkbox clicked : ChexkBox_1 click事件中进行一些更改后,您必须使用$(this)而不是document.getElementById("HiddenFields")处理当前单击的复选框:

$('.ChexkBox_1').click(function () {
    if (!$(this).is(':checked')) {
        $(this).parents('table').find(".HiddenFields_1").css('visibility',"hidden");
    }
    else {
        $(this).parents('table').find(".HiddenFields_1").css('visibility',"visible");
    }
});

NOTE : when you clone the row you have to change id because element IDs should be unique within the entire document. 注意:克隆行时,您必须更改id因为元素ID在整个文档中应该是唯一的。

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

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