Javascript 计算数量,其中textbox为特定值
编辑 这么多好的答案,而且都很有效!非常感谢大家:)我希望我能把所有问题都标记为已解决 --- 你好 假设我有两个文本输入:Javascript 计算数量,其中textbox为特定值,javascript,jquery,html,loops,Javascript,Jquery,Html,Loops,编辑 这么多好的答案,而且都很有效!非常感谢大家:)我希望我能把所有问题都标记为已解决 --- 你好 假设我有两个文本输入: <input type="text" id="plt_quantity_sum"/> <!-- this should calculate the "#quantity" where each "#uom_value" is "PLT" --> <input type="text" id="crt_quantity_sum"/><
<input type="text" id="plt_quantity_sum"/> <!-- this should calculate the "#quantity" where each "#uom_value" is "PLT" -->
<input type="text" id="crt_quantity_sum"/><!-- this should calculate the "#quantity" where each "#uom_value" is "CRT" -->
让我们假设以下场景:
项目名称
计量单位
量
项目1
PLT
5.
项目2
PLT
3.
项目3
阴极射线管
2.
项目4
阴极射线管
3.
您可以尝试此代码。我没有测试它
var plt_count = 0;
var crt_count = 0;
$(".uom_value").each(function() {
if($(this).html === 'PLT'){
plt_count += parseInt($(this).closest('.qty').html());
}
if($(this).html === 'CRT'){
crt_count += parseInt($(this).closest('.qty').html());
}
});
$("#plt_quantity_sum").val(plt_count);
$("#crt_quantity_sum").val(crt_count);
你可以试试这个代码。我没有测试它
var plt_count = 0;
var crt_count = 0;
$(".uom_value").each(function() {
if($(this).html === 'PLT'){
plt_count += parseInt($(this).closest('.qty').html());
}
if($(this).html === 'CRT'){
crt_count += parseInt($(this).closest('.qty').html());
}
});
$("#plt_quantity_sum").val(plt_count);
$("#crt_quantity_sum").val(crt_count);
除了纠正Hamza指出的拼写错误外,我认为您应该基本上遍历给定其类名的元素
document.getElementsByClassName('.someclass')
,然后使用class'.qty'存储并求和其每个同级的值
然后获取该值并使用它填充所需的输入
希望有帮助;) 除了纠正Hamza指出的拼写错误外,我想说的是,您应该基本上迭代给定其类名的元素document.getElementsByClassName('.someclass')
,然后用类'.qty'存储和求和其每个同级的值
然后获取该值并使用它填充所需的输入
希望有帮助;) 您只需为两个和声明两个变量crtQtySum
和pltQtySum
,并将它们初始化为0
,然后在tds上循环,检查它是否为crt
或plt
,并相应地更新您的变量:
$(document).ready(function() {
var crtQtySum = 0;
var pltQtySum = 0;
$(".uom_value").each(function() {
if ($(this).text() === "CRT") {
crtQtySum += parseInt($(this).next("td.qty").text());
} else if ($(this).text() === "PLT") {
pltQtySum += parseInt($(this).next("td.qty").text());
}
});
$("#plt_quantity_sum").val(pltQtySum);
$("#crt_quantity_sum").val(crtQtySum);
});
$(文档).ready(函数(){
var crtQtySum=0;
变量pltQtySum=0;
$(“.uom_值”)。每个(函数(){
如果($(this).text()=“CRT”){
crtQtySum+=parseInt($(this).next(“td.qty”).text());
}else if($(this).text()=“PLT”){
pltQtySum+=parseInt($(this).next(“td.qty”).text();
}
});
$(“plt数量总和”).val(pltQtySum);
$(“crt数量总和”).val(crtQtySum);
});代码>
项目名称
计量单位
量
项目1
PLT
5.
项目2
PLT
3.
项目3
阴极射线管
2.
项目4
阴极射线管
3.
PLT:
CRT:
您只需为这两个和声明两个变量crtQtySum
和pltQtySum
,并将它们初始化为0
,然后在tds上循环,检查它是CRT
还是plt
,并相应地更新您的变量:
$(document).ready(function() {
var crtQtySum = 0;
var pltQtySum = 0;
$(".uom_value").each(function() {
if ($(this).text() === "CRT") {
crtQtySum += parseInt($(this).next("td.qty").text());
} else if ($(this).text() === "PLT") {
pltQtySum += parseInt($(this).next("td.qty").text());
}
});
$("#plt_quantity_sum").val(pltQtySum);
$("#crt_quantity_sum").val(crtQtySum);
});
$(文档).ready(函数(){
var crtQtySum=0;
变量pltQtySum=0;
$(“.uom_值”)。每个(函数(){
如果($(this).text()=“CRT”){
crtQtySum+=parseInt($(this).next(“td.qty”).text());
}else if($(this).text()=“PLT”){
pltQtySum+=parseInt($(this).next(“td.qty”).text();
}
});
$(“plt数量总和”).val(pltQtySum);
$(“crt数量总和”).val(crtQtySum);
});代码>
项目名称
计量单位
量
项目1
PLT
5.
项目2
PLT
3.
项目3
阴极射线管
2.
项目4
阴极射线管
3.
PLT:
CRT:
这可以使用很多方法来完成,这是其中之一:
$(document).ready(function(){
var sum_PLT = 0, sum_CRT = 0;
$('table > tbody > tr').each(function() {
tr = $(this)[0];
cells = tr.cells;
if(cells[0].textContent != "Item Name"){//To exclude the <th>
if(cells[1].textContent == "PLT")
sum_PLT += parseInt(cells[2].textContent);
else
sum_CRT += parseInt(cells[2].textContent);
}
});
$("#plt_quantity_sum").val(sum_PLT);
$("#crt_quantity_sum").val(sum_CRT);
});
$(文档).ready(函数(){
var和PLT=0,和CRT=0;
$('table>tbody>tr')。每个(函数(){
tr=$(此)[0];
细胞=tr细胞;
如果(单元格[0].textContent!=“项目名称”){//排除
if(单元格[1].textContent==“PLT”)
sum_PLT+=parseInt(单元格[2].textContent);
其他的
sum_CRT+=parseInt(单元格[2].textContent);
}
});
美元(“#plt#u数量_总和”).val(sum#plt);
$(“数量和”).val(数量和和);
});
这是一项工作。这可以使用多种方法来完成,这是其中之一:
$(document).ready(function(){
var sum_PLT = 0, sum_CRT = 0;
$('table > tbody > tr').each(function() {
tr = $(this)[0];
cells = tr.cells;
if(cells[0].textContent != "Item Name"){//To exclude the <th>
if(cells[1].textContent == "PLT")
sum_PLT += parseInt(cells[2].textContent);
else
sum_CRT += parseInt(cells[2].textContent);
}
});
$("#plt_quantity_sum").val(sum_PLT);
$("#crt_quantity_sum").val(sum_CRT);
});
$(文档).ready(函数(){
var和PLT=0,和CRT=0;
$('table>tbody>tr')。每个(函数(){
tr=$(此)[0];
细胞=tr细胞;
如果(单元格[0].textContent!=“项目名称”){//排除
if(单元格[1].textContent==“PLT”)
sum_PLT+=parseInt(单元格[2].textContent);
其他的
sum_CRT+=parseInt(单元格[2].textContent);
}
});
美元(“#plt#u数量_总和”).val(sum#plt);
$(“数量和”).val(数量和和);
});
这是一个正在运行的代码。您可能需要尝试此代码
<script>
$(document).ready(function(){
var plt_qty = 0;
var crt_qty = 0;
$('.uom_value').each(function(){
if ($(this).text() === 'PLT' ) {
plt_qty = plt_qty + parseInt($(this).parent().find('.qty').text());
}else if ($(this).text() === 'CRT' ) {
crt_qty = crt_qty + parseInt($(this).parent().find('.qty').text());
}
});
$("#plt_quantity_sum").val(plt_qty);
$("#crt_quantity_sum").val(crt_qty);
});
</script>
$(文档).ready(函数(){
var plt_数量=0;
var crt_数量=0;
$('.uom_值')。每个(函数(){
如果($(this).text()=='PLT'){
plt_qty=plt_qty+parseInt($(this).parent().find('.qty').text());
}else if($(this).text()='CRT'){
crt_qty=crt_qty+parseInt($(this).parent().find('.qty').text());
}
});
美元(“#plt#u数量_总和”).val(plt#u数量);
$(“crt数量总和”).val(crt数量);
});
注意:删除uom
中的类uom\u值
,您可能需要尝试此代码
<script>
$(document).ready(function(){
var plt_qty = 0;
var crt_qty = 0;
$('.uom_value').each(function(){
if ($(this).text() === 'PLT' ) {
plt_qty = plt_qty + parseInt($(this).parent().find('.qty').text());
}else if ($(this).text() === 'CRT' ) {
crt_qty = crt_qty + parseInt($(this).parent().find('.qty').text());
}
});
$("#plt_quantity_sum").val(plt_qty);
$("#crt_quantity_sum").val(crt_qty);
});
</script>
$(文档).ready(函数(){
var plt_数量=0;
var crt_数量=0;
$('.uom_值')。每个(函数(){
如果($(this).text()=='PLT'){
plt_qty=plt_qty+parseInt($(this).parent().find('.qty').text());
}else if($(this).text()='CRT'){
crt_qty=crt_qty+parseInt($(this).parent().find('.qty').text());
}
});
美元(“#plt#u数量_总和”).val(plt#u数量);
$(“crt数量总和”).val(crt数量);
});
注意:在uom
中删除classuom\u value
首先,您有两个输入文本具有相同的id=plt\u quantity\u sum
是,但我尚未测试,这只是一个拼写错误。我会修好的。谢谢你的帮助