2014-10-10 41 views
1

我使用函數來克隆帶有輸入字段的html錶行,以允許用戶輸入多個輸入。克隆行時清除隱藏的輸入

HTML看起來像這樣

<table id="ID_1"> 
    <tr id="tr_id1"> 
     <td><input type="hidden" value="databaseid"/></td> 
     <td>Inputfield 1</td> 
     <td>Inputfield 2 </td> 
    </tr> 
</table> 
<button onclick="cloneRow('ID_1', 'tr_id1')"></button> 

和我的JavaScript

function cloneRow(tablename,rowname) { 
    var row = document.getElementById(rowname); // find row to copy 
    var table = document.getElementById(tablename); // find table to append to 
    var clone = row.cloneNode(true); // copy children too 
    clone.id = "newID"; // change id or other attributes/contents 
    table.appendChild(clone); // add new row to end of table 
    $('.pickDate').each(function() { 
     $(this).datepicker({ dateFormat: 'dd.mm.yy' }); 
    }); 
} 

進一步開展工作的下一個頁面上,我只需要第一<td><input type="hidden" value="databaseid"/></td>所以通過複製,它應該只複製隱藏的輸入,但沒有側面的值,值應該看起來像value=""

這怎麼解決? (jQuery的可以使用)

小提琴http://jsfiddle.net/21sv1bug/

+0

正確縮進代碼,如果可能的話提供小提琴(http://jsfiddle.net/) – 2014-10-10 17:24:51

+0

@NitsanBaleli我只是照顧的縮進...另一方面,我看不出有什麼理由,爲什麼這應該是一個問題沒有小提琴測試... – webeno 2014-10-10 17:27:05

+0

@NitsanBaleli http://jsfiddle.net/21sv1bug/ – 2014-10-10 17:28:40

回答

1
$(row).find('input:hidden').val('') 

使用上面的函數調用之後

+0

這工作後放置在正確的地方:)!謝謝 – 2014-10-10 17:40:50

0

您可以清空輸入的值與這個jQuery代碼行中:

$(row).find('input').val('') 
0

試試這個:

function cloneRow(tablename,rowname) { 
    var value=$(this).prev('table').find('input:hidden').val(); // store the value in a variable 
    $(this).prev('table').find('input:hidden').val(''); // empty the input 
    var row = document.getElementById(rowname); // find row to copy 
    var table = document.getElementById(tablename); // find table to append to 
    var clone = row.cloneNode(true); // copy children too 
    clone.id = "newID"; // change id or other attributes/contents 
    table.appendChild(clone); // add new row to end of table 
    $(this).prev('table').find('input:hidden').val(value); // put the value in its place again 
    $('.pickDate').each(function() { 
     $(this).datepicker({ dateFormat: 'dd.mm.yy' }); 
    }); 
} 

UPDATE:

一個Working DEMO使用jQuery:

$('button').click(function(){ 
    var clone=$(this).prev('table').find('tr:first').clone(); 
    clone.attr('id','newID'); 
    clone.find('input:hidden').val(''); 
    $(this).prev('table').append(clone); 
}); 
+0

可悲的是沒有改變... – 2014-10-10 17:32:29