首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >检查可变数量的html文本输入的值的和

检查可变数量的html文本输入的值的和
EN

Stack Overflow用户
提问于 2011-09-14 15:33:17
回答 2查看 336关注 0票数 0

我正在构建一个购物车,每个产品至少有2种颜色和5种不同的大小。当有人订购这种产品时,他必须填写以下表格:

代码语言:javascript
复制
<table cellspacing="0">
            <thead>
                <tr>
                    <th class="tcl" scope="col">COLOUR/SIZE</th>
                    <th class="tc2" scope="col">34</th>
                    <th class="tc3" scope="col">36</th>
                    <th class="tcr" scope="col">38</th>
                    <th class="tcr4" scope="col">40</th>
                    <th class="tcr4" scope="col">42</th>

                    <th class="tcr4" scope="col">44</th>
                    <th class="tcr4" scope="col">48</th>
                </tr>
            </thead>
            <tbody>
            <tr class="tcl">
                <td class="tcl">
                <div class="intd">

                    <div class="tclcon">
                    <img src="http://mlhpro.29media.eu/uploads/produse/mlh-gloria-gown-black-mlh-2e299d-pm.jpg" width="20" height="20" border="0" /><br />
                    black</div>
                </div>
                </td>
                <td class="tc2"><input type="text" name="qty_black_34" value="" size="1" border="noborder" style="background-color:#F2ECDB; text-align:right;" /></td>
                <td class="tc3"><input type="text" name="qty_black_36" value="" size="1" border="noborder" style="background-color:#F2ECDB; text-align:right;" /></td>
                <td class="tcr"><input type="text" name="qty_black_38" value="" size="1" border="noborder" style="background-color:#F2ECDB; text-align:right;" /></td>

                <td class="tcr4"><input type="text" name="qty_black_40" value="" size="1" border="noborder" style="background-color:#F2ECDB; text-align:right;" /></td>
                <td class="tcr4"><input type="text" name="qty_black_42" value="" size="1" border="noborder" style="background-color:#F2ECDB; text-align:right;" /></td>
                <td class="tcr4"><input type="text" name="qty_black_44" value="" size="1" border="noborder" style="background-color:#F2ECDB; text-align:right;" /></td>
                <td class="tcr4"><input type="text" name="qty_black_48" value="" size="1" border="noborder" style="background-color:#F2ECDB; text-align:right;" /></td>
            </tr>
            <tr class="tcl">
                <td class="tcl">
                <div class="intd">
                    <div class="tclcon">

                    <img src="http://mlhpro.29media.eu/uploads/produse/mlh-gloria-gown-powder-mlh-e6847a-pm.jpg" width="20" height="20" border="0" /><br />
                    powder</div>
                </div>
                </td>
                <td class="tc2"><input type="text" name="qty_powder_34" value="" size="1" border="noborder" style="background-color:#F2ECDB; text-align:right;" /></td>
                <td class="tc3"><input type="text" name="qty_powder_36" value="" size="1" border="noborder" style="background-color:#F2ECDB; text-align:right;" /></td>
                <td class="tcr"><input type="text" name="qty_powder_38" value="" size="1" border="noborder" style="background-color:#F2ECDB; text-align:right;" /></td>
                <td class="tcr4"><input type="text" name="qty_powder_40" value="" size="1" border="noborder" style="background-color:#F2ECDB; text-align:right;" /></td>

                <td class="tcr4"><input type="text" name="qty_powder_42" value="" size="1" border="noborder" style="background-color:#F2ECDB; text-align:right;" /></td>
                <td class="tcr4"><input type="text" name="qty_powder_44" value="" size="1" border="noborder" style="background-color:#F2ECDB; text-align:right;" /></td>
                <td class="tcr4"><input type="text" name="qty_powder_48" value="" size="1" border="noborder" style="background-color:#F2ECDB; text-align:right;" /></td>
            </tr>
            <tr class="tcl">
                <td class="tcl">
                <div class="intd">
                    <div class="tclcon">
                    <img src="http://mlhpro.29media.eu/uploads/produse/mlh-gloria-gown-red-mlh-959b5f-pm.jpg" width="20" height="20" border="0" /><br />

                    red</div>
                </div>
                </td>
                <td class="tc2"><input type="text" name="qty_red_34" value="" size="1" border="noborder" style="background-color:#F2ECDB; text-align:right;" /></td>
                <td class="tc3"><input type="text" name="qty_red_36" value="" size="1" border="noborder" style="background-color:#F2ECDB; text-align:right;" /></td>
                <td class="tcr"><input type="text" name="qty_red_38" value="" size="1" border="noborder" style="background-color:#F2ECDB; text-align:right;" /></td>
                <td class="tcr4"><input type="text" name="qty_red_40" value="" size="1" border="noborder" style="background-color:#F2ECDB; text-align:right;" /></td>
                <td class="tcr4"><input type="text" name="qty_red_42" value="" size="1" border="noborder" style="background-color:#F2ECDB; text-align:right;" /></td>

                <td class="tcr4"><input type="text" name="qty_red_44" value="" size="1" border="noborder" style="background-color:#F2ECDB; text-align:right;" /></td>
                <td class="tcr4"><input type="text" name="qty_red_48" value="" size="1" border="noborder" style="background-color:#F2ECDB; text-align:right;" /></td>
            </tr>
                        </tbody>
</table>

因此,我的输入字段的命名遵循以下约定:"qty_" + "color name" + "size"

检查输入值的总和是否至少为3的最简单方法是什么?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-09-14 15:53:52

看起来你可以对表中的所有输入求和。应该是:

代码语言:javascript
复制
var total = 0;
$.each($("input"),function(i, obj){
  total += parseInt($(obj).val()) || 0;
});
if(total > 2){
  return true;
}
return false;

如果您有其他麻烦的字段,您可以通过在table元素上使用.find来限制集合,或者使用更具体的输入选择器来检查名称:

代码语言:javascript
复制
$("input[name^='qty_']")

编辑必须解决两件事。我已经订购了obj, i in each rails,你想要i, obj

此外,还必须使用|| 0处理空输入

请参阅working JSfiddle here

票数 2
EN

Stack Overflow用户

发布于 2011-09-14 16:02:20

这将显示每行上所有输入的总和

代码语言:javascript
复制
$( "table tbody tr" ).each( function() {
    var sum = 0;
    $(this).find( "td:gt(0) input[type=text]" ).each( function() {
        sum += $(this).val() * 1;
    });
    alert(sum)
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7412785

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档