2013-10-03 30 views
0

我在主页面中使用了asp.net。我有一个产生8行的列表视图。我想用jQuery来做的事情是,当有人在单元格1-7中输入一个值时,当他们离开单元格时,我想计算单元格1-7并将该值放入单元格8中。所以每行都会有计算完成。我发现了一些代码来循环表使用JQuery来将行中的单元格总数设置为

enter code here 

$(document).ready(function() { 
$('#ctl00_ContentPlaceHolder1_lvOccLine_ctrl0_tbltblOccLineList tr').each(function() { 
    $(this).find('td').each(function() { 
    }) 
}) 

});

但是还没有取得任何进展。在firebug中,我看到我想要获得的值在this/cells/1/childnodes中。它看起来像这样

NodeList[input#ctl00_ContentPlaceHolder1_lvOccLine_ctrl0_txtCacasian attribute value = "1"] 

的HTML渲染这个样子的

<input type="text" style="width:100%;" id="ctl00_ContentPlaceHolder1_lvOccLine_ctrl0_txtCacasian" value="1" name="ctl00$ContentPlaceHolder1$lvOccLine$ctrl0$txtCacasian"> 

任何帮助将是巨大的

回答

0

我说实话不是太熟悉ASP.NET,但总体而言jQuery的,你可以简单地通过CLASSES获取单元格来完成这个任务,并像这样更新第8个单元格?

例如, 1)把一个公共类为您的第7个细胞,即“细胞 - 取” 2)把一类独特的最后第八细胞,即“小区-8” 3)对细胞7(模糊=事件的内容http://api.jquery.com/blur/)的模糊事件 只是做一个简单的抓取并添加:

$(this).find('.cell-to-fetch').each(function() { 
    $total += $(this).val(); 
}) 

4)最后只是更新你的-8和您,这样的结果:

$('.cell-8').val($total); 

希望它有帮助,至少作为一个普遍的概念。

0

Demo

$(document).ready(function() { 
    $('#ctl00_ContentPlaceHolder1_lvOccLine_ctrl0_tbltblOccLineList input').on('change', function() { /* bind change to input */ 
     var sum = 0, 
      $this = $(this).parents('tr'); 
     $this.find('input').each(function() { /* find all inputs in the row */ 
      var value = parseInt(this.value); 
      sum += value % 1 == 0 ? value : 0; /* add values together */ 
     }); 
     $this.find('td').last().text(sum); /* output sum into last column */ 
     return true; 
    }); 
}); 
+0

感谢您的代码..我得到的JavaScript运行时错误对象不支持属性或方法错误。我正在使用指向http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js的jquery。也指向jquery-ui.min.js。它可能是我没有指出正确的jQuery文件? – jvcoach23

+0

@ jvcoach23您正在使用尚未使用'on'方法的过时jQuery版本。我认为那是在那个时候“绑定”的。 –

+0

是的..我更新了jquery,现在它没有错误。它不是我所需要的......它将所有1-7列和所有行加在一起。我需要第1行第1-7列的总数,并且总数在第8列。然后对第2行执行相同的操作。代码包含来自第0列的输入,我不需要这样做。我会继续努力的。感谢让我开始 – jvcoach23

0

http://jsfiddle.net/dKxW8/

$(document).ready(function() { 
    $("#calc").click(function() { 
     //first get number of rows in the table (because we have one input per row) 
     var count = $("#mytable tr").length; 

     //loop through the rows and get the sum of every input value except the last 
     var sum = 0; 
     for (var i = 0; i < (count - 1); i++) { 
      //get the value and add it to sum 
      //check if its a number 
      if(!isNaN(parseInt($("#mytable tr").eq(i).find("input").val(), 10))){ 
       sum += parseInt($("#mytable tr").eq(i).find("input").val(), 10); 
      } 
     } 

     //assign the last input's value (in last row) to the sum 
     $("#mytable tr").eq(count - 1).find("input").val(sum); 
    }); 
}); 
相关问题