Copying values of one table to another (how to convert this js function to jQuery)

半城伤御伤魂 提交于 2019-12-13 19:41:08

问题


I am stuck with a small problem here.. What i am trying to do is copy description of id's from one table to another. I have writter half of the javascript and can anybody tell me how to convert this function in jquery. I want the description copied from the first table based on the id to the second table. Have done this in jquery using 'contains', (Comparing 2 tables column values and copying the next column content to the second table) since there are 1000 table rows, the explorer crashes. Is there a way to simplify it ??... the code is as follows...

the current javascript works when i click on test in the second table, but i want the value to be appended in the second table on page load... pls help

<table class="reportTabe">
<tr><td>psx-pdu120v1</td><td class="itemname" id="psx-pdu120v1">some description1</td></tr>
<tr><td>psx-pdu120v1</td><td class="itemname" id="psx-pdu120v1">some description1</td></tr>
<tr><td>psx-pdu120v3</td><td class="itemname" id="psx-pdu120v3">some description3</td></tr>
<tr><td>psx-pdu120v4</td><td class="itemname" id="psx-pdu120v4">some description4</td></tr>
<tr><td>psx-pdu120v5</td><td class="itemname" id="psx-pdu120v5">some description5</td></tr>
<tr><td>psx-pdu120v6</td><td class="itemname" id="psx-pdu120v6">some description6</td></tr>
<tr><td>psx-pdu120v7</td><td class="itemname" id="psx-pdu120v7">some description7</td></tr>
<tr><td>psx-pdu120v8</td><td class="itemname" id="psx-pdu120v8">some description8</td></tr>
<tr><td>psx-pdu120v9</td><td class="itemname" id="psx-pdu120v9">some description9</td></tr>
</table>

<table class="data">
<tr><td class="whipItem">psx-pdu120v1</td><td onClick="Javascript:alert(document.getElementById('psx-pdu120v1').innerText)";>test</td></tr>
<tr><td class="whipItem">psx-pdu120v3</td><td onClick="Javascript:alert(document.getElementById('psx-pdu120v1').innerText)";>test</td></tr>
<tr><td class="whipItem">psx-pdu120v4</td><td onClick="Javascript:alert(document.getElementById('psx-pdu120v5').innerText)";>test</td></tr>
<tr><td class="whipItem">psx-pdu120v5</td><td Javascript:this.innerText=document.getElementById('psx-pdu120v4').innerText;></td></tr>
<tr><td class="whipItem">psx-pdu120v6</td><td Javascript:this.innerText=document.getElementById('psx-pdu120v5').innerText;></td></tr>
<tr><td class="whipItem">psx-pdu120v7</td><td Javascript:this.innerText=document.getElementById('psx-pdu120v6').innerText;></td></tr>
<tr><td class="whipItem">psx-pdu120v8</td><td Javascript:this.innerText=document.getElementById('psx-pdu120v7').innerText;></td></tr>
<tr><td class="whipItem">psx-pdu120v9</td><td Javascript:this.innerText=document.getElementById('psx-pdu120v8').innerText;></td></tr>
</table>

回答1:


$(document).ready(function() {
        $('.whipItem', '.data').each(function(index, element) { //for each whipItem in the data table
            var name = $(element).text(); //get the text value
            var desc = $(".itemname[id='" + name + "']").text(); //get the description whose id matches the name in the report table.
            $(element).next().text(desc); //change the value of the next td to the description.
        });
    });



回答2:


$(function() {

        $.each($('firstTable td'), function(i) {
            var tableData = $(this);
            $('.secondTable td').eq(i).text(tableData.text());
        });
    });


来源:https://stackoverflow.com/questions/2526003/copying-values-of-one-table-to-another-how-to-convert-this-js-function-to-jquer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!