页面有如下table:
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="1" class="contentTable">
<tr>
<td height="28" width="25%" class="fieldName" align="center">元素排列顺序</td>
<td height="28" width="25%" class="fieldName" align="center">元素项</td>
<td height="28" width="25%" class="fieldName" align="center">元素值</td>
<td height="28" width="25%" class="fieldName" align="center">元素长度</td>
</tr>
<c:forEach var="voucherFlowRule" items="${voucherFlowRule}">

<tr>
<td height="28" width="25%"  align="center" class="fieldContent">${voucherFlowRule.elementOrder}</td>
<td height="28" width="25%"  align="center" class="fieldContent">${voucherFlowRule.elementDesc}</td>
<td height="28" width="25%"  align="center" class="fieldContent">${voucherFlowRule.elementValue}</td>
<td height="28" width="25%"  align="center" class="fieldContent">${voucherFlowRule.elementLength}</td>
</tr>
</c:forEach>
   </table>
   <input type="button" name="viewFlow" value="预览合并后的元素值" class="btn" onClick="reviewFlowNum()"/>&nbsp;&nbsp;
<input tye="text" id="element" class="fieldContent"/>
请问怎么将table中所有的元素值合并并输出到element中,按钮实现的javascript函数reviewFlowNum怎么写?

解决方案 »

  1.   

    就是将table中所有<td height="28" width="25%"  align="center" class="fieldContent">${voucherFlowRule.elementValue}</td>
    合并输出到input中
      

  2.   

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
        <title>Untitled Page</title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
    <script type="text/javascript">
    function reviewFlowNum(){
    var i ='';
    $("table tr:gt(0) td:nth-child(3)").each(function(){
    i += $(this).text();
    });
    $("#element").val(i);
    }
    </script>
    </head>
    <body>
    <table width="100%" border="0" align="center" cellpadding="0" cellspacing="1" class="contentTable">
                <tr>
                    <td height="28" width="25%" class="fieldName" align="center">元素排列顺序</td>
                    <td height="28" width="25%" class="fieldName" align="center">元素项</td>
                    <td height="28" width="25%" class="fieldName" align="center">元素值</td>
                    <td height="28" width="25%" class="fieldName" align="center">元素长度</td>
                </tr>          
                <tr>
                    <td height="28" width="25%"  align="center" class="fieldContent">${voucherFlowRule.elementOrder}</td>
                    <td height="28" width="25%"  align="center" class="fieldContent">${voucherFlowRule.elementDesc}</td>
                    <td height="28" width="25%"  align="center" class="fieldContent">1</td>
                    <td height="28" width="25%"  align="center" class="fieldContent">${voucherFlowRule.elementLength}</td>
                </tr>
    <tr>
                    <td height="28" width="25%"  align="center" class="fieldContent">${voucherFlowRule.elementOrder}</td>
                    <td height="28" width="25%"  align="center" class="fieldContent">${voucherFlowRule.elementDesc}</td>
                    <td height="28" width="25%"  align="center" class="fieldContent">2</td>
                    <td height="28" width="25%"  align="center" class="fieldContent">${voucherFlowRule.elementLength}</td>
                </tr>            
    <tr>
                    <td height="28" width="25%"  align="center" class="fieldContent">${voucherFlowRule.elementOrder}</td>
                    <td height="28" width="25%"  align="center" class="fieldContent">${voucherFlowRule.elementDesc}</td>
                    <td height="28" width="25%"  align="center" class="fieldContent">3</td>
                    <td height="28" width="25%"  align="center" class="fieldContent">${voucherFlowRule.elementLength}</td>
                </tr>            
    <tr>
                    <td height="28" width="25%"  align="center" class="fieldContent">${voucherFlowRule.elementOrder}</td>
                    <td height="28" width="25%"  align="center" class="fieldContent">${voucherFlowRule.elementDesc}</td>
                    <td height="28" width="25%"  align="center" class="fieldContent">4</td>
                    <td height="28" width="25%"  align="center" class="fieldContent">${voucherFlowRule.elementLength}</td>
                </tr>            
    <tr>
                    <td height="28" width="25%"  align="center" class="fieldContent">${voucherFlowRule.elementOrder}</td>
                    <td height="28" width="25%"  align="center" class="fieldContent">${voucherFlowRule.elementDesc}</td>
                    <td height="28" width="25%"  align="center" class="fieldContent">5</td>
                    <td height="28" width="25%"  align="center" class="fieldContent">${voucherFlowRule.elementLength}</td>
                </tr>            
    <tr>
                    <td height="28" width="25%"  align="center" class="fieldContent">${voucherFlowRule.elementOrder}</td>
                    <td height="28" width="25%"  align="center" class="fieldContent">${voucherFlowRule.elementDesc}</td>
                    <td height="28" width="25%"  align="center" class="fieldContent">6</td>
                    <td height="28" width="25%"  align="center" class="fieldContent">${voucherFlowRule.elementLength}</td>
                </tr>            
    <tr>
                    <td height="28" width="25%"  align="center" class="fieldContent">${voucherFlowRule.elementOrder}</td>
                    <td height="28" width="25%"  align="center" class="fieldContent">${voucherFlowRule.elementDesc}</td>
                    <td height="28" width="25%"  align="center" class="fieldContent">7</td>
                    <td height="28" width="25%"  align="center" class="fieldContent">${voucherFlowRule.elementLength}</td>
                </tr>            
    <tr>
                    <td height="28" width="25%"  align="center" class="fieldContent">${voucherFlowRule.elementOrder}</td>
                    <td height="28" width="25%"  align="center" class="fieldContent">${voucherFlowRule.elementDesc}</td>
                    <td height="28" width="25%"  align="center" class="fieldContent">8</td>
                    <td height="28" width="25%"  align="center" class="fieldContent">${voucherFlowRule.elementLength}</td>
                </tr>
                </tr>
                  </table>
                  <input type="button" name="viewFlow" value="预览合并后的元素值" class="btn" onClick="reviewFlowNum()"/>&nbsp;&nbsp;
    <input type="text" id="element" class="fieldContent"/>
    </body>
    </html>
      

  3.   

    function reviewFlowNum(){
                var r =[];
                $("table tr:gt(0) td:nth-child(3)").each(function(){
                    r.push( $(this).text());
                });
                $("#element").val(r.join('')); // r.join(',')
            }用数组 push join 性能是远远高于字符串拼接的
      

  4.   

    非大数据量字符串拼接 没有必要使用数组的push功能。
      

  5.   

    怎么指定表明啊,我页面里面有好几个table
      

  6.   

     $("table tr:gt(0) td:nth-child(3)")如果有ID
    table => #id
    如果有class且唯一 .class
    如果有name
    table[name='你的name']