将组中的复选框项乘以相对数字字段
Multiplying checkbox item in a group to relative number field
我有一个代码,可以在选中时将复选框项乘以数字字段。当一个复选框数组中有多个项目时,它只会乘以第一个项目。如何分隔复选框组?是否应该为每个项目创建一个唯一的名称和脚本?
JSFiddle 这里
这是我的 HTML:
<input type="checkbox" class="1" value="30" name="1" />
<div style="display:none;" class="checked-1">
<input type="number" name="product_1_qty[]" id="product_1_qty" placeholder="Quantity" class=" qty" value="0" />
</div>
<p>Total: PHP <span id='total'>0</span></p>
这是脚本
$(document).ready(function () {
var sum = 0;
var qty = 0;
$("input[type=checkbox]").change(function () {
if ($('.1').is(':checked')) {
$('.checked-1').show();
} else {
$('.checked-1').hide();
}
});
$('.qty').bind('keyup mouseup', function () { // if user typed anyting in the Quantity
sum = parseInt($('.1').val()); // get checkbox value and change the data type to int
qty = parseInt($(this).val()); // get Quantity value and change the data type to int
//console.log(sum,qty);
if(sum && qty) { // if the values was not empty
$('#total').text(sum * qty); // show the result in the total element
} else { // if the values was empty
$('#total').val(''); // clear the result textbox
}
});
});
我想在一个复选框数组中有多个项目
<input type="checkbox" class="1" value="30" name="1" />
<div style="display:none;" class="checked-1">
<input type="number" name="product_1_qty[]" id="product_1_qty" placeholder="Quantity" class=" qty" value="0" />
</div>
<p>Total: PHP <span id='total'>0</span></p>
<input type="checkbox" class="1" value="30" name="1" />
<div style="display:none;" class="checked-1">
<input type="number" name="product_1_qty[]" id="product_1_qty" placeholder="Quantity" class=" qty" value="0" />
</div>
<p>Total: PHP <span id='total'>0</span></p>
<input type="checkbox" class="1" value="30" name="1" />
<div style="display:none;" class="checked-1">
<input type="number" name="product_1_qty[]" id="product_1_qty" placeholder="Quantity" class=" qty" value="0" />
</div>
<p>Total: PHP <span id='total'>0</span></p>
当我这样做时,代码出错了。提前感谢!
如果你要重复相同的HTML块,你应该使用类而不是ID(因为ID必须是唯一的),然后使用jQuery的树遍历方法来找到正确的表单元素。
$(document).ready(function() {
var sum = 0;
var qty = 0;
$("input[type=checkbox]").change(function() {
if ($(this).is(':checked')) {
$(this).next('.checked-1').show();
} else {
$(this).next('.checked-1').hide();
}
});
$('.qty').bind('keyup mouseup', function() { // if user typed anyting in the Quantity
sum = parseInt($(this).parent().prev('.1').val()); // get checkbox value and change the data type to int
qty = parseInt($(this).val()); // get Quantity value and change the data type to int
//console.log(sum,qty);
if (sum && qty) { // if the values was not empty
$(this).parent().next('p').find('.total').text(sum * qty); // show the result in the total element
} else { // if the values was empty
$(this).parent().next('p').find('.total').val(''); // clear the result textbox
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" class="1" value="30" name="1" />
<div style="display:none;" class="checked-1">
<input type="number" name="product_1_qty[]" id="" placeholder="Quantity" class=" qty" value="0" />
</div>
<p>Total: PHP <span class='total'>0</span>
</p>
<input type="checkbox" class="1" value="30" name="1" />
<div style="display:none;" class="checked-1">
<input type="number" name="product_1_qty[]" id="" placeholder="Quantity" class=" qty" value="0" />
</div>
<p>Total: PHP <span class='total'>0</span>
</p>
相关文章:
- 我可以获得相对于被点击元素的确切点击位置吗
- 扩展移相器按钮类不工作
- 将图像上传ajax与表单提交ajax相结合
- 固定位置菜单时滚动,直到它击中一个相对容器的底部
- 可以用'吗/'在相对路径中
- JavaScript:单击时相对于父级增加变量值
- 在不移动元件的情况下从相对位置更改为固定位置
- 具有相同类的jquery-click元素
- 打开相对于鼠标位置的CSS3/HTML5模式对话框
- 将输入文本与某个选项的值相匹配并自动选择
- 如何在Javascript中将字符串与模式数组相匹配
- 抵消在具有pageY计算的相对容器中位于顶部
- 使用不同的超时对数组中的每个项目进行角度动画处理
- AngularJS:删除重复的对象并按值对数组进行重新排序
- 将语句与jquery相结合,并使用媒体查询来实现返回页首按钮
- 相对于他在jQuery/Js中的一个内部数据对数组进行排序
- 将组中的复选框项乘以相对数字字段
- 对数组进行排序并返回一个指示符数组,该数组指示已排序的元素相对于原始元素(new)的位置
- 如何使滑块的高度与图像相对于窗口宽度的高度相匹配
- Javascript/html5 2d Canvas Context -获取相对于Canvas的X, Y(相对于转换对象