如何在多个div中获取具有特定类名的html元素值的总和具有相同的类名 [英] How to get sum of html element's values with specific class names in the in multiple divs have same class name

查看:70
本文介绍了如何在多个div中获取具有特定类名的html元素值的总和具有相同的类名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在多个div中获取具有特定类名的html元素值的总和具有相同的类名。

How to get sum of html element''s values with specific class names in the multiple divs have same class name.

<html>
<link REL="STYLESHEET" TYPE="text/css" HREF="checkoperations.css">


<script type="text/javascript" src="jquery.js"/>&lt;/script>
<script type="text/javascript" src="checkoperations.js"></script>
<body>

<input type="button" id="calculate" value="calculate"/>
<div class="testdiv">
      <input type='checkbox' name='checked2' value='check' class='ckbox'>
      <input type='text' class ='fieldsbankrecon1' readonly='readonly' value='7'/>
      <input type='text' class ='fieldsbankrecon' readonly='readonly' value='8'/>

</div>

<div class="testdiv"  >
      <input type='checkbox' name='checked' value='check' class='ckbox'>
      <input type='text' class ='fieldsbankrecon1' readonly='readonly' value='1' />
      <input type='text' class ='fieldsbankrecon2' readonly='readonly' value='2' />

</div>


<div class="testdiv"  >
      <input type='checkbox' name='checked' value='check' class='ckbox'>
      <input type='text' class ='fieldsbankrecon1' readonly='readonly' value='1' />
      <input type='text' class ='fieldsbankrecon2' readonly='readonly' value='2' />

</div>

<div class="testdiv"  >
      <input type='checkbox' name='checked' value='check' class='ckbox'>
      <input type='text' class ='fieldsbankrecon1' readonly='readonly' value='1' />
      <input type='text' class ='fieldsbankrecon2' readonly='readonly' value='2' />

</div>

<body>

</html>



jQuery code


jQuery code

var sum = 0;
$('#calculate').click(function()
    {

    $('.testdiv').each(function() {
    //var $this = $(this);
    //if ($this.is(':checked')) {
        var currentValue = parseInt($(".fieldsbankrecon1").val(),10);
        sum = sum  + currentValue;
        //}
         alert(message);

    });



消息总是7,这是第一个文本框div。

它虽然迭代了四次,但我不知道为什么价值没有改变。



我猜我的消息没有完全显示在这里。我的HTML代码有四个具有相同类名的div,即 testdiv ,每个div中有三个输入框。



我非常感谢帮助。


Message is always 7 which is the first text box in the first div.
It''s iterating four times though, but I don''t know why the value is not changing.

I guess my message is not completely shown here. My HTML code has four divs with same class name that is testdiv and inside each div there are three input boxes.

I would really appreciate the help.

推荐答案

' #calculate')。click( function ()
{
('#calculate').click(function() {


' 。testdiv')。each( function () {
// var
('.testdiv').each(function() { //var


this =


这篇关于如何在多个div中获取具有特定类名的html元素值的总和具有相同的类名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆