使用jquery动态地将行添加到表中

时间:2013-09-25 09:18:15

标签: jquery

我想将它添加到一个表中但是当我点击添加按钮时,两行同时添加我的意思是循环运行两次。但删除行没有问题。

$(document).ready(function() {
    $(".add_partners_details").click(function() {
    var newrow = $('<div class="t-row"><div class="t-col"><input type="text" class="text_field txtNumeric" name="partners_or_directors_sl_no_id[]" value="" placeholder=""/></div><div class="t-col"><input type="text" class="text_field txtNumeric" name="partners_or_directors_pin_id[]" value="" placeholder=""/></div><div class="t-col end"><input type="text" class="text_field required txtAlphabet" name="partners_or_directors_name_id[]" value="" placeholder=""/></div><div class="t-col border_none"><a href="javascript:void(0);" class="remove_partners_details">Remove</a></div></div>');
        $("#details_of_partners_directors").append(newrow);
    });

    $("#details_of_partners_directors").on('click','.remove_partners_details',function() {
        $(this).parent().parent().remove();
    });
});

HTML

<div class="t-row">
    <div class="t-col">
         <input type="text" class="text_field txtNumeric"name="partners_or_directors_sl_no_id[]" value="" placeholder=""/>
    </div>
    <div class="t-col">
         <input type="text" class="text_field txtNumeric" name="partners_or_directors_pin_id[]" value="" placeholder=""/>
    </div>
    <div class="t-col end">
         <input type="text" class="text_field required txtAlphabet" name="partners_or_directors_name_id[]" value="" placeholder=""/>
    </div>
    <div class="t-col border_none">
         <a href="javascript:void(0);" class="add_partners_details">Add</a>
    </div>
</div>

3 个答案:

答案 0 :(得分:0)

我怀疑这种情况正在发生,因为你正在使用一个类选择器,它会导致click事件为具有该类的每个元素触发。尝试使用唯一的类名或ID。

答案 1 :(得分:0)

我使用effect()后遇到的唯一问题,你还需要包含jQuery UI库

<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.3/themes/redmond/jquery-ui.css">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.3/jquery-ui.js"></script>

演示:Plunker

答案 2 :(得分:0)

我做了一些工作,请检查

Jquery:

<script type="text/javascript">
    $(document).ready(function () {

        $(".add_partners_details").click(function () {
            var curindex = parseInt($("#hdn").val(), 10);
            if (curindex == 0) {
                curindex = 1;
            }
            else {
                curindex = curindex + 1;
            }

            var newDiv = '<div class="t-row" id= parentDiv' + curindex + '>' +
    '<div class="t-col">' +
                '<input type="text" class="text_field txtNumeric" name="partners_or_directors_sl_no_id[]"' +
            'value="" placeholder="" /></div>' +
    '<div class="t-col">' +
        '<input type="text" class="text_field txtNumeric" name="partners_or_directors_pin_id[]" value="" placeholder="" /></div>' +
    '<div class="t-col end" id="last">' +
        '<input type="text" class="text_field required txtAlphabet" name="partners_or_directors_name_id[]" value="" placeholder="" /></div>' +
    '<div class="t-col border_none">' +
        '<a href="javascript:void(0);" class="add_partners_details">Add</a>' +
        '<a href="javascript:void(0);" class="Remove_partners_details">Remove</a>' +
    '</div>' +
'</div>';
            var prediv = $('#hdnParent').val();
            $("#" + prediv).after(newDiv);
            $("#hdn").val(curindex);
            $('#hdnParent').val('parentDiv' + curindex);

        });

        $(".Remove_partners_details").click(function () {

            var curindex = parseInt($("#hdn").val(), 10);
            if (curindex != 0)
                $("#" + $('#hdnParent').val()).remove();
            if (curindex >= 1) {
                $("#" + curindex).remove();
                if (curindex == 1) {
                    curindex = 0;
                }
                else {
                    curindex = curindex - 1;
                }
                if (curindex == 0) {
                    $('#hdnParent').val('parentDiv');

                }
                else {
                    $('#hdnParent').val('parentDiv' + curindex);

                }
                $("#hdn").val(curindex);
            }

        });

    });
</script>

<强> ASPX:

   <div class="t-row" id="parentDiv">
    <div class="t-col">
        <input type="text" class="text_field txtNumeric"   name="partners_or_directors_sl_no_id[]"
            value="" placeholder="" /></div>
    <div class="t-col">
        <input type="text" class="text_field txtNumeric" name="partners_or_directors_pin_id[]"
            value="" placeholder="" /></div>
    <div class="t-col end" id="last">
        <input type="text" class="text_field required txtAlphabet" name="partners_or_directors_name_id[]"
            value="" placeholder="" /></div>
    <div class="t-col border_none">
        <a href="javascript:void(0);" class="add_partners_details">Add</a>
        <a href="javascript:void(0);" class="Remove_partners_details">Remove</a>
    </div>
  </div>
  <input type="hidden" id="hdn" value="0"/>
 <input type="hidden" id="hdnParent" value="parentDiv"/>

我的工作正常。现在你必须只添加隐藏和显示删除和添加按钮的逻辑。作为动态控件,您必须使用http://api.jquery.com/live/触发事件。请这样做,因为我有一些时间限制。

希望这会对你有所帮助。请更新以获得进一步的帮助。