【问题标题】:jQuery to loop through table rows and cells, where checkbox is checked, then concatenatejQuery循环遍历表格行和单元格,选中复选框,然后连接
【发布时间】:2013-04-23 13:58:11
【问题描述】:

我有一个包含多行的表。表格中有几列带有复选框。我需要遍历每个复选框,并在其中选中将来自该特定单元格的输入连接/连接到同一行的其他输入。

你可以在这里看到一个小提琴: http://jsfiddle.net/QS56z/10/

除了循环遍历行之外,我如何循环遍历每个<td> 一旦我有这个<td> 我如何在那个td 中查找名称以x 开头的输入。一旦找到将输入从该输入连接/连接到行中的输入。

我希望这是有道理的。

基本上: 我想将(家庭)加入(大小)到(等级),家庭和等级在同一行,并且每行有几个大小。每个结果都必须写入当前正在处理的 TD。

我已经走到了这一步,但卡住了:

function createcodes(){
    alert('running');
    //run through each row
    $('.authors-list tr').each(function(){
         //processing this row
            //how to process each cell(table td) where there is checkbox
        $($(this).find('input[name^="line"]').val(

            $('$(this).find('input[name^="family"]'').val() + ' ' + // common input(family) on row, use for all table cells(td)
            $('#$(this).find('input[name^="size"]'').val() + ', ' + // this cells input called size, unique to this cell only
            $('#$(this).find('input[name^="grade"]'').val() // common input(grade) on row, use for all table cells(td)
          );
          // end of cell row processing
      });
        //end of rows processing
}

一如既往的感谢。

http://jsfiddle.net/QS56z/10/

我的 html 是:

<table class="authors-list" border=1 id="ordertable">
 <tr>
     <td ><input type="text" id="product1" name="product1" class="rounded" value="38114CR"></td>
     <td ><input type="text" size='5' id="qty1" name="qty1" class="rounded" value="10"/></td> 
     <td class="tdcheckbox">
      <input type="checkbox"  id="h09_1" name="h09_1" checked class="rounded"/>
      <input type="text"  id="line_1_09" name="line_1_09" >
      <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" checked class="rounded"/>
      <input type="text"  id="line_1_15" name="line_1_15" >
      <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" value="38114"></td>
    <td><input type="text" name="skugrade_1" id="skugrade_1" value="cr"></td>
 </tr>
</table>
<input type="button" id="continue" value="continue">

请记住有多个行。谢谢。

【问题讨论】:

    标签: jquery


    【解决方案1】:

    更新

    我已经更新了你的演示:http://jsfiddle.net/terryyounghk/QS56z/18/

    另外,我已将两个^= 更改为*=。见http://api.jquery.com/category/selectors/

    并注意:checked 选择器。见http://api.jquery.com/checked-selector/

    function createcodes() {
    
        //run through each row
        $('.authors-list tr').each(function (i, row) {
    
            // reference all the stuff you need first
            var $row = $(row),
                $family = $row.find('input[name*="family"]'),
                $grade = $row.find('input[name*="grade"]'),
                $checkedBoxes = $row.find('input:checked');
    
            $checkedBoxes.each(function (i, checkbox) {
                // assuming you layout the elements this way, 
                // we'll take advantage of .next()
                var $checkbox = $(checkbox),
                    $line = $checkbox.next(),
                    $size = $line.next();
    
                $line.val(
                    $family.val() + ' ' + $size.val() + ', ' + $grade.val()
                );
    
            });
    
        });
    }
    

    【讨论】:

    • 特里,这让我很生气。这么简单的答案。非常感谢!
    • @Smudger 不客气。我认为这符合您的描述?这适合标记为答案吗?
    • 100% 正确。抱歉,我目前的连接不可靠。 :-) 再次感谢特里!
    • @Smudger 明白了。谢谢你。干杯
    • 嗨,特里,这很棒,但不是 100% 正确。在小提琴上,它不是采用当前 的大小,而是每次都采用第一个 。我们如何纠正它以获取它正在引用的当前 。此外,我怎样才能让它只在选中复选框的情况下工作?再次感谢。
    【解决方案2】:

    试试这个:

    function createcodes() {
    
        $('.authors-list tr').each(function () {
            //processing this row
            //how to process each cell(table td) where there is checkbox
            $(this).find('td input:checked').each(function () {
    
                 // it is checked, your code here...
            });
        });
    }
    

    【讨论】:

    • 谢谢,这会查看所有复选框还是仅选中复选框?我需要它来检查复选框...谢谢Palash
    • 嗨帕拉什,我已经更新了我的小提琴尝试。 jsfiddle.net/QS56z/12 请您进一步指导我...谢谢。
    猜你喜欢
    • 2016-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多