javascript - table之間點擊循環傳遞值
問題描述
現在有兩個table,table1 和table2 初始化狀態的時候,table中有數據,點擊checkbox,數據傳遞到table2中,并且table1的數據刪除,這時候,如果點擊table2的checkbox,數據重新傳遞到table1中。
效果圖如下:
我能從table1中將數據傳遞到table2,代碼如下
$('#table1 input').click(function () {//var html=$(this).parent().parent();//$('#table2').append(html);});$('#table2 input').click(function(){var html=$(this).parent().parent();//$('#table1').append(html);});
主要使用這樣的方法,現在問題在于,將tr整個對象傳遞過去之后,在點擊table2中的checkbox的時候,tr的對象傳遞不了,測試,顯示都沒有進入點擊事件,這個是怎么回事??在線等,謝謝大家
問題解答
回答1:var otable1=$(’#table1’),otable2=$(’#table2’);otable1.on(’click’,’input’,function(){//var otr= $(this).parent();//otable2.append(otr); otr.remove();})otable2.on(’click’,’input’,function(){//var otr= $(this).closest('li');//otable1.append(otr);//otr.remove();})
試下(保證#table1,#table1是一開始在頁面上的,不是未來元素),如果不行。代碼貼出來,幫你測試下!
回答2:DEMO
<!DOCTYPE html><html><head>//<meta charset='utf-8'>//<meta name='viewport' content='width=device-width'><title>JS Bin</title>//<script src='https://code.jquery.com/jquery-3.1.0.js'></script> <style>table {border: 1px solid red;padding: 5px;}</style></head><body><table id='table1'><tr> <td><input type=’checkbox’ />123123</td></tr></table><table id='table2'><tr></tr> </table><script>$(document).on(’click’,’#table1 input’,function(){//var html = $(this).parent();$('#table2').append(html);})$(document).on(’click’,’#table2 input’,function(){var html = $(this).parent();//$('#table1').append(html);}) </script></body></html>
如果可行請采納哦,3Q。
相關文章:
1. javascript - 關于<a>元素與<input>元素的JS事件運行問題2. css3 - 純css實現點擊特效3. docker start -a dockername 老是卡住,什么情況?4. java - 為什么第一個線程已經釋放了鎖,第二個線程卻不行?5. mysql - 記得以前在哪里看過一個估算時間的網站6. javascript - vscode alt+shift+f 格式化js代碼,通不過eslint的代碼風格檢查怎么辦。。。7. 大家好,我想請問一下怎么做搜索欄能夠搜索到自己網站的內容。8. python - 啟動Eric6時報錯:’qscintilla_zh_CN’ could not be loaded9. html - vue項目中用到了elementUI問題10. mysql - 查詢字段做了索引為什么不起效,還有查詢一個月的時候數據都是全部出來的,如果分拆3次的話就沒問題,為什么呢。
