欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

用Jquery获取table中td的值

程序员文章站 2022-05-25 22:38:17
...
最近做项目时需要用jquery获取table下的值,在网上搜索了一下发现不错,于是就copy了一下 附件是原作者写的

jquery代码(jquery 读取table tr td值完成修改功能 )

var $td=$(td).parents('tr').children('td');
$("#seshu").val($td.eq(0).find('input').val());
$("#jshuchudan").val($td.eq(1).find('input').val());
$("#jshuchuqi").val($td.eq(2).find('input').val());
$("#jchuantongddan").val($td.eq(3).find('input').val());
$("#jchuantongdqi").val($td.eq(4).find('input').val());
$("#schuantongddan").val($td.eq(5).find('input').val());
$("#schuantongdqi").val($td.eq(6).find('input').val());
$("#zhizuodan").val($td.eq(7).find('input').val());
$("#zhizuoqi").val($td.eq(8).find('input').val());
$("#ganbandan").val($td.eq(9).find('input').val());
$("#ganbanqi").val($td.eq(10).find('input').val());
$("#pinbandan").val($td.eq(11).find('input').val());
$("#pinbanqi").val($td.eq(12).find('input').val());
$("#jjid").val($td.eq(13).find('input').val());

$("#updateform").submit();
用Jquery获取table中td的值:

          首先建立一个如下一个两行三列的table

           <table id="tb" style="text-align: center;" width="100%" border="1PX">
                <tr> <td>A1  </td><td> A2 </td> <td>A3</td> </tr>
                <tr><td>B1</td> <td>  B2</td><td>B3</td> </tr>
           </table>

        Jquery 代码部分

              <SCRIPT type=text/javascript src="Jquery/jquery-1.4.2.js">//引入Jquery 插件不能少哦</SCRIPT>

              <SCRIPT language="javascript" type="text/javascript" >

                $(function(){
                    //使用jquery 取得table 中td里面的值
                       $("#tb td").click(function(){
                                      alert($(this).text());

                         })
           
                });</SCRIPT >

用Javascript 获取table中td的值:

      直接就写JS拉,借用上面table

      <script language="javascript" type="text/javascript">
        function GetTdValue()
        {
            //Javascript 取得table 中TD的值
            var VAL=document.getElementById("tb").rows[1].cells[1];
            alert(VAL.innerHTML)
        }
    </script>




因为这个tr需要动态增加和删除,并且获得td中的值

<script src="js/jquery-1.3.2.js" type="text/javascript"> </script>
<script type="text/javascript">
function addit(){
    var n = $("#paramTable tr:last td").eq(1).html();
    if(isNaN(n)) n = 1;
    else n++;
    $("#paramTable").append("<tr><td>name</td><td>" + n + "</td><td onclick='delit(this);'>删除</td></tr>")
}
function delit(obj){ $(obj).parent().remove();} </script>
<input type="button" onclick="addit()" value="添加">
<table border=1 id="paramTable">
<tr>
<td width=100>
参数名称
</td>
<td width=100>
对应列号
</td>
<td width=60>
是否删除
</td>
</tr>
<tr>
<td>name</td>
<td>1</td>
<td onclick="delit(this);">删除</td>
</tr>
</table>






function getMerchandiseTable(){
            var arrMerchandise = [];
            $("#tbOrderMerchandise tr").each(function(){
                 var $MerchandiseIdTd = $("td:eq(0)",$(this) );//商品ID
                
                 var $quantityTd = $("td:eq(3)",$(this) );//数量
                 var $priceTd=$("td:eq(6)",$(this) );//小计
                 var $DescriptionTd = $("td:eq(7)",$(this) );//备注
               
                if($MerchandiseIdTd.text()!="")
                {
                    arrMerchandise.push($MerchandiseIdTd.text()+"|"+$quantityTd.text()+"|"+$priceTd.text()+"|"+$DescriptionTd.text());
                }
            });
            $("#hidDetailList").val(arrMerchandise.join(","));
        }



private List<hanvnet.Dto.Page.Table.DeliveryFeePage> GetDeliveryFeeList(string strChargerId)
    {
        string strFeeInput = hdFeeList.Value;

        List<hanvnet.Dto.Page.Table.DeliveryFeePage> lstFee
            = new List<hanvnet.Dto.Page.Table.DeliveryFeePage>();
        hanvnet.Dto.Page.Table.DeliveryFeePage deliveryFeePage = null;

        string[] strFeeList = strFeeInput.Split(Char.Parse(","));
        for (int i = 0; i < strFeeList.Length; i++)
        {
            deliveryFeePage = new hanvnet.Dto.Page.Table.DeliveryFeePage();
            string[] strFeeDetailList = strFeeList[i].Split(Char.Parse("|"));
            deliveryFeePage.DeliveryId = _deliveryId;
            deliveryFeePage.FeeId = int.Parse(strFeeDetailList[0]);
            deliveryFeePage.ReceiveId = strChargerId;
            deliveryFeePage.PlanAmount = decimal.Parse(strFeeDetailList[1]);
            deliveryFeePage.Description = strFeeDetailList[2];
            deliveryFeePage.DataStatus = 1;
            deliveryFeePage.InsDateTime = DateTime.Now;
            deliveryFeePage.InsUserId =       this.Session[hanvnet.Common.Utils.Constraints.C_SESSION_LOGINID].ToString();
          lstFee.Add(deliveryFeePage);
        }
        return lstFee;
    }
function UnOffOrderAmount(){
        //alert("不优惠");
          var UnoffOrderAmounts=0;
          $("#tbOrderMerchandise tr").each(function(){
                 var $priceTd=$("td:eq(6)",$(this) );//优惠金额
              
                UnoffOrderAmounts+=$priceTd.text()*1;
              
            });
            return UnoffOrderAmounts;
        }

<td class="tablealigncenter" nowrap>
        <input type="text" name="countId" id="countId" onblur="getAgentAccountMoney(this)"></>
    </td>

//alert($($($AgentMoney)).find("input").get(0).value);


function getBillsTable(){
                var arrbills = [];
                $("#mainTableBody tr").each(function(){
                var $productId=$("td:eq(1)",$(this) ); 
                var $baseGoldPrice=$("td:eq(5)",$(this));
                var $baseGoldAcountPrice=$("td:eq(6)",$(this));
                var $AgentMoney=$("td:eq(7)",$(this));
                 //alert($($($AgentMoney)).find("input").get(0).value);
                var $AgentAccountMoney=$("td:eq(8)",$(this));
                // alert($($AgentAccountMoney).find("input").get(0).value);
                 if($productId!=null){
                  arrbills.push($productId.text()+"/"+$baseGoldPrice.text()+"/"+$baseGoldAcountPrice.text()+"/"+$($AgentMoney).find("input").get(0).value+"/"+$($AgentAccountMoney).find("input").get(0).value);
                  }
                });
                $("#hidTableValue").val(arrbills.join(","));
                $("#createfrom").attr("action","<c:url value='/k4_oldGoldStorsge/saveFactoryBills'/>");
                $("#createfrom").submit();
          }