【问题标题】:jquery to loop through table, for each row and td concatonate valuesjquery循环遍历表,为每一行和td连接值
【发布时间】:2013-04-23 22:44:07
【问题描述】:

我有一个有几行的表。每行有一个产品字段、一个等级字段和一个家庭字段。

每个可用尺寸都有几个复选框。

表格中的一行如下所示:

<table class="authors-list" border=0 id="ordertable">
 <tr>
     <td style="font-size:10px;"><a class="deleteRow"> <img src="<?php echo base_url() ?>application/assets/images/delete2.png" /></a></td>
     <td ><input type="text" id="product1" name="product1" class="rounded"/></td>
     <td ><input type="text" size='5' id="qty1" name="qty1" class="rounded"/></td> 
     <td class="tdcheckbox">
      <input type="checkbox"  id="h09_1" name="h09_1" class="rounded"/>
      <input type="text"  id="line_1_09" name="line_1_09" value="">
      <input type="text"  id="size_1_09" name="size_1_09" value="09">

    </td> 
     <td class="tdcheckbox">
      <input type="checkbox"  id="h12_1" name="h12_1" class="rounded"/>
      <input type="text"  id="line_1_12" name="line_1_12" value="">
      <input type="text"  id="size_1_12" name="size_1_12" value="12">
    </td> 
     <td class="tdcheckbox">
      <input type="checkbox"  id="h15_1" name="h15_1" class="rounded"/>
      <input type="text"  id="line_1_15" name="line_1_15" value="">
      <input type="text"  id="size_1_15" name="size_1_15" value="15">
    </td> 
    <td><input type="text" name="cubespercheck_1" id="cubespercheck_1" value="0" size=5></td>
    <td><input type="text" name="skufamily_1" id="skufamily_1"></td>
    <td><input type="text" name="skugrade_1" id="skugrade_1"></td>
 </tr>
</table>
<input type="button" id="continue" value="continue">

除了产品字段之外,所有字段都将隐藏在最终结果中。

为您提供背景知识,我的产品代码如下所示: 3811460S5 38114是宽度和高度 60是长度 Cr是等级。

我想要发生的是,当我单击按钮时,jquery 必须遍历所有表格单元格。如果有一个 CHECKED 复选框并且已经填充了产品,jquery 必须加入如下字段: skufamily(for row[skufamily])+length(from cell[size])+grade(from row[skugrade])

此结果必须填充当前 td 输入 line。因此,现在每个单元格将具有一个精确的SKU(仅在检查和填充产品时)

skufamily、size 和 Grade 已预先填充,因此只需要 jquery 循环并加入字段。

这是一个可以演奏的小提琴。 http://jsfiddle.net/QS56z/

我尝试了以下概念,但无法完全确定代码。

    function createcodes() {
$("table.authors-list").find('input[type="checkbox"][name^="h"]:checked').each(function () {
 if (<checkbox is checked>)
      document.getElementById(input[type="skufamily").value + 
      document.getElementById(input[type="size").value +
      document.getElementById(input[type="skugrade").value

        });

这远非正确,所以如果你能让我走上正确的道路,我将不胜感激。

一如既往地感谢一百万。

【问题讨论】:

  • 你自己有没有尝试过?
  • 感谢 Limelights,我已经用我所拥有的内容更新了这个问题,但我认为我走错了路……非常抱歉……对于 jquery 和一般编码来说是新手。
  • 作为初学者无需道歉,只需确保您在发布之前阅读了我们的常见问题解答。常见问题解答:stackoverflow.com/faq
  • 感谢 Limelights,今后会继续坚持下去。
  • 酷豆,酋长! :)

标签: jquery


【解决方案1】:

更新了您的fiddle。 我认为这段代码应该可以做到。

$("#continue").click(function(){
    $("table#ordertable > tbody > tr").each(function(){
        var productVal = $('td:eq(0) input', this).val();
        if(productVal.length > 0){
            //get the code portion
            var trimmedProductVal = productVal.substring(0, productVal.length - 2);
            var productCode = productVal.substring(productVal.length-2, productVal.length);
            //get the checked items
            $('td.tdcheckbox', this).each(function(){
                var currentCell = this;
                $("input[type='checkbox']:checked", this).each(function(){
                    //concatenate the value
                    var valueToSet = $('input[type="text"]:eq(1)', currentCell).val();
                    valueToSet = trimmedProductVal + valueToSet + productCode;
                    //set the text value
                    $('input[type="text"]:eq(0)', currentCell).val(valueToSet);
                });;
            });
        }
    });
});

【讨论】:

  • 令人印象深刻。谢谢特伦斯,我需要解决这个问题才能理解它,但效果很好。欣赏你的努力。再次感谢。
猜你喜欢
  • 2011-03-09
  • 1970-01-01
  • 2014-06-29
  • 1970-01-01
  • 2015-04-14
  • 1970-01-01
  • 2016-01-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多