如何使用 jQuery 创建基于复选框的计算函数?
我的表中有几个复选框(数量未指定),并为每个复选框设置值 2000
。 我希望选中的每个复选框都乘以 2000
,如果未选中,则从总金额中扣除 2000
?
例如,如果选中 3 个复选框,则数字 3 乘以 2000
,3 * 2000
。 而如果不勾选这个数字的话,它的值会从总数中扣除吗?
并在span
中显示总值?
<table class="table">
<thead>
<tr>
<th>
choose
</th>
</tr>
</thead>
<tbody>
@foreach (var item in listitem)
{
<tr>
<td>
<input class="check-horse mr-2" type="checkbox" autocomplete="off" />
</td>
</tr>
}
</tbody>
total price :<span class="text-success totalprice"></span>
I have several checkboxes in my table (unspecified number) and set a value of 2000
for each checkbox.
I want each check box that was checked to be multiplied by 2000
and if it was unchecked 2000
be deducted from the total amount?
For example, if 3 checkboxes are checked, the number 3 is multiplied by 2000
,3 * 2000
.
And if it is unchecked from this number, its value will be deducted from the total?
And display the total value in span
?
<table class="table">
<thead>
<tr>
<th>
choose
</th>
</tr>
</thead>
<tbody>
@foreach (var item in listitem)
{
<tr>
<td>
<input class="check-horse mr-2" type="checkbox" autocomplete="off" />
</td>
</tr>
}
</tbody>
total price :<span class="text-success totalprice"></span>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您只需使用
:checked
伪选择器即可获取选中的复选框,它应该按预期工作。
You can get the checked checkbox simply by using
:checked
pseudo selectorIt should work as expected.
详细信息在下面的示例中注释。请注意,每个复选框的值已指定为 2000。此外,使用
代替
,它是一个内联标记并且可以包含类似
的文本,但与
不同,
也可以有
value
就像表单控件一样(例如、
Details are commented in example below. Note, the value of each checkbox has been assigned as 2000. Also instead of a
<span>
, an<output>
is used -- it is an inline tag and can contain text like a<span>
, but unlike the<span>
an<output>
can also have avalue
like a form-control (ex.<input>
,<select>
, etc.).