一旦标签都具有值,就动态地对标签的值求和
Dynamically sum the values of labels once they all have values
我有一系列 asp Radiobuttons
可以很好地填充标签的值,但现在使用 jQuery 检查,如果所有标签都有值,那么我想对这些值进行动态求和.
使用 clsTxtToCalculate
class 或引用标签值,code/function 会实现此目的吗?
<tr>
<td>
<asp:Label ID="lblScore1" CssClass="center clsTxtToCalculate" clientidmode="Static" runat="server" AutoCompleteType="None" />
</td>
<td>
<asp:Label ID="lblScore2" CssClass="center clsTxtToCalculate" clientidmode="Static" runat="server" AutoCompleteType="None" />
</td>
<td>
<asp:Label ID="lblScore3" CssClass="center clsTxtToCalculate" clientidmode="Static" runat="server" AutoCompleteType="None" />
</td>
<td>
<asp:Label ID="lblScore4" CssClass="center clsTxtToCalculate" clientidmode="Static" runat="server" AutoCompleteType="None" />
</td>
</tr>
您可以遍历标签并对值求和:
function notify() {
var labels = $(".clsTxtToCalculate");
var total = 0;
$.each(labels, function(index,lbl){
total += parseInt($(lbl).text());
});
alert(total)
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<table>
<tr>
<td>
<Label ID="lblScore1" class="center clsTxtToCalculate">10</label>
</td>
<td>
<Label ID="lblScore2" class="center clsTxtToCalculate">20</label>
</td>
<td>
<Label ID="lblScore3" class="center clsTxtToCalculate">30</label>
</td>
<td>
<Label ID="lblScore4" class="center clsTxtToCalculate">40</label>
</td>
</tr>
</table>
<button onclick="notify()">Calculate</button>
在你的收音机更改事件中(我猜你有其中之一来填充标签),你可以在填充标签文本后做这样的事情:
var labels = $('.clsTxtToCalculate'),
labelsWithText = labels.filter(function() {
return $(this).text() != '';
});
if (labels.length == labelsWithText.length) {
// do calculation here
}
我有一系列 asp Radiobuttons
可以很好地填充标签的值,但现在使用 jQuery 检查,如果所有标签都有值,那么我想对这些值进行动态求和.
使用 clsTxtToCalculate
class 或引用标签值,code/function 会实现此目的吗?
<tr>
<td>
<asp:Label ID="lblScore1" CssClass="center clsTxtToCalculate" clientidmode="Static" runat="server" AutoCompleteType="None" />
</td>
<td>
<asp:Label ID="lblScore2" CssClass="center clsTxtToCalculate" clientidmode="Static" runat="server" AutoCompleteType="None" />
</td>
<td>
<asp:Label ID="lblScore3" CssClass="center clsTxtToCalculate" clientidmode="Static" runat="server" AutoCompleteType="None" />
</td>
<td>
<asp:Label ID="lblScore4" CssClass="center clsTxtToCalculate" clientidmode="Static" runat="server" AutoCompleteType="None" />
</td>
</tr>
您可以遍历标签并对值求和:
function notify() {
var labels = $(".clsTxtToCalculate");
var total = 0;
$.each(labels, function(index,lbl){
total += parseInt($(lbl).text());
});
alert(total)
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<table>
<tr>
<td>
<Label ID="lblScore1" class="center clsTxtToCalculate">10</label>
</td>
<td>
<Label ID="lblScore2" class="center clsTxtToCalculate">20</label>
</td>
<td>
<Label ID="lblScore3" class="center clsTxtToCalculate">30</label>
</td>
<td>
<Label ID="lblScore4" class="center clsTxtToCalculate">40</label>
</td>
</tr>
</table>
<button onclick="notify()">Calculate</button>
在你的收音机更改事件中(我猜你有其中之一来填充标签),你可以在填充标签文本后做这样的事情:
var labels = $('.clsTxtToCalculate'),
labelsWithText = labels.filter(function() {
return $(this).text() != '';
});
if (labels.length == labelsWithText.length) {
// do calculation here
}