简体   繁体   English

jQuery将表行复制到另一个表

[英]Jquery copy table row to another table

I have some form elements in a table(1) that I would like add to another table(2) and also a jquery ui pop up that has a table(3) with items to add to table(2). 我在table(1)中有一些表单元素,我想将其添加到另一个table(2)中,还弹出一个jQuery ui,其中包含一个table(3),其中要添加到table(2)的项目。 Basically a manually entered list or a list you can choose from some pre entered items. 基本上是一个手动输入的列表,或者您可以从一些预先输入的项目中选择一个列表。

I have this working to a certain level however the problem I have now is when I add an item from the pop up list, remove it and add another item from the pop list it duplicates the items into table(2). 我在某种程度上可以正常工作,但是现在的问题是,当我从弹出列表中添加一个项目,将其删除并从弹出列表中添加另一个项目时,它将项目复制到table(2)中。

http://jsfiddle.net/b5mmf/1/ http://jsfiddle.net/b5mmf/1/

Not sure this is the best way of copying rows between tables. 不确定这是在表之间复制行的最佳方法。 As when I do this I need to ignore certain columns and change the add button to a delete button. 在执行此操作时,我需要忽略某些列,并将添加按钮更改为删除按钮。

 $('#viewquote').click(function() {
        $('#dialog-confirm').dialog({
        resizable: false,
        maxWidth:920,
                maxHeight: 'auto',
                width: 920,
                height: 'auto',
        modal: true,
        buttons: {
        Ok: function() {
        $( this ).dialog( "close" );

        },

        }
        }); 
 $('.importitem').click(function(){

    var quoterow = $(this).closest('tr').html();
    var quoterowid = $(this).closest('tr').attr('id');


    $("#"+quoterowid+" img").css("display", "block");

 $('#purchaseitemstable tbody').append('<tr id='+quoterowid+'>'+quoterow+'</tr>');
$("#purchaseitemstable td[id*='ignore']").html("");
$("#purchaseitemstable td[id*='button']").html("<input name='deleteitem' type='button' class='deleteitem'  value='Remove' />");

});         
});


 $(document).on('click', '.deleteitem' , function() {
        var quoterow ="";
        var removeitemid = $(this).closest('tr').attr('id');


        $('#'+ removeitemid).remove();
});


var count = 0;
    $('.additem').click(function() {

        var newitemqty = $("#purchaseitemsqty").val();
        var newitemdescription = $("#purchaseitemsdescription").val();
        var newitemcost = $("#purchaseitemscost").val();
        var newitemmancode = $("#purchaseitemsmancode").val();
        count++;

    $('#purchaseitemstable tbody').append('<tr id=item_'+ count +'><td   valign="top">\
    <input name="purchaseitemsqty" value="'+newitemqty+'" size="5" type="text"  ></td>\
    <td  valign="top"><input type="text" name="purchaseitemsdescription" value="'+newitemdescription+'" ></td>\
 <td valign="top"> <input type="text" name="purchaseitemsmancode" id="purchaseitemsmancode" value="'+newitemmancode+'"> </td> <td  valign="top"><input name="purchaseitemscost" value="'+newitemcost+'" size="7" type="text"><div> <input name="deleteitem" type="button" class="deleteitem"  value="Remove" /></div></td></tr>');
 });

You need to move out the click event of .importitem out of viewquote click. 你需要迁出的单击事件.importitemviewquote点击。

Instead of this 代替这个

$('#viewquote').click(function() {
    $('#dialog-confirm').dialog('open');        
        $('.importitem').click(function(){
           ..................
           ....................
        });
});

What happens in the above code is each time the pop up is clicked it binds a click event .importitem . 上面的代码中发生的事情是每次单击弹出窗口都会绑定一个click事件.importitem So if you open popup 3 times the code in the .importitem click event will be executed three times. 因此,如果您打开弹出窗口3次,则.importitem click事件中的代码将执行3次。

Try like this : 尝试这样:

$('#viewquote').click(function() {
    $('#dialog-confirm').dialog('open');        
});

$('.importitem').click(function(){
.........
.........
});

See working demo here 这里查看工作演示

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

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