在我的应用程序中,人们可以按照他们想要的数量填写产品旁边的输入字段来订购产品。
在同一个列表中,当客户订购产品时,还有一个特定的部分带有'测试人员',比方说6件,他们可以免费获得1个测试人员。人们可以购买更多的测试人员,但付钱给他们。
我正在努力使用下面的代码。我需要做的是检查'tester'输入是否有值,这不是问题。如果我们有4个测试仪输入值,那么事情会变得更加复杂。假设客户可以订购2个免费测试人员,第一个和第二个输入值为“1”(金额),第三个输入值为“2”。我只想获得前两个“产品”以将其排除在总价。 2个测试人员的数量是可变的。
<table>
<tr><td><input min="0"type="number"data-price="16.50" size="1" id="retail" value="" placeholder="0" name="583"></td></tr>
<tr><td><input min="0"type="number"data-price="16.50" size="1" id="tester" value="" placeholder="0" name="584"></td></tr>
<tr><td><input min="0"type="number"data-price="8.50" size="1" id="tester" value="" placeholder="0" name="585"></td></tr>
<tr><td><input min="0"type="number"data-price="4.50" size="1" id="tester" value="" placeholder="0" name="586"></td></tr>
<tr><td><input min="0"type="number"data-price="8.50" size="1" id="tester" value="" placeholder="0" name="587"></td></tr>
<tr><td><input min="0"type="number"data-price="2.50" size="1" id="tester" value="" placeholder="0" name="588"></td></tr>
</table>
代码:
// has the value of the total free test products a customer can buy
totalFreeTestProducts = (Math.floor(counter))
$("table tr td input").each(function(index) {
// product price
price = $(this).data('price');
// id is category
if ($(this).attr('id') == 'testers') {
// amount value is not 0
if ($(this).val() > 0) {
// Do something, sum the products based on
// the amount of free testers to buy.
}
}
});
答案 0 :(得分:0)
您多次使用ID tester
,您应该使用元素上的类&amp;然后使用
$(this).hasClass('tester'); // tester is class name
始终记住ID应该是唯一的。
答案 1 :(得分:0)
这是你需要的吗?
var retailPrice = 0;
var testerPrice =0;
$("input").on("change", function () {
var sum = 0;
if ($(this).attr('id') == 'retail') {
var qty = $(this).val();
if (qty == (6 * Math.floor(qty / 6))) {
var freeTesterQty = $("#testerFree").val();
if (freeTesterQty === "") {
freeTesterQty = parseInt(0);
}
freeTesterQty = (6 * Math.floor(qty / 6))/6;
$("#testerFree").val(freeTesterQty);
}else{
freeTesterQty = (6 * Math.floor(qty / 6))/6;
if(freeTesterQty == 0){
freeTesterQty="";
}
$("#testerFree").val(freeTesterQty);
}
retailPrice = $(this).attr('data-price') * qty;
//sum += retailPrice;
} else if ($(this).attr('id') == 'tester') {
var qty = $(this).val();
var freeTesterQty = $("#testerFree").val();
if (freeTesterQty === "") {
freeTesterQty = parseInt(0);
}
testerPrice = $(this).attr('data-price') * qty;
//sum += testerPrice;
}
sum = retailPrice+testerPrice;
$(".sum").html(sum + "€")
});
在这个例子中,免费测试仪出现在每6个产品中。
更新1:
对于多个产品输入:
更新2:
以下是多个产品/测试人员输入的JS代码:
var retailPrice = 0;
var testerPrice = 0;
$("input").on("change", function () {
var sum = 0;
if ($(this).attr('class') == 'retail') {
retailPrice = 0;
var qty = 0;
$(".retail").each(function(){
var currRetailQty = $(this).val();
if(currRetailQty == ""){
currRetailQty = 0;
}
qty += parseInt(currRetailQty);
retailPrice += $(this).attr('data-price') * currRetailQty;
});
if (qty == (6 * Math.floor(qty / 6))) {
var freeTesterQty = $("#testerFree").val();
if (freeTesterQty === "") {
freeTesterQty = parseInt(0);
}
freeTesterQty = (6 * Math.floor(qty / 6))/6;
$("#testerFree").val(freeTesterQty);
}else{
freeTesterQty = (6 * Math.floor(qty / 6))/6;
if(freeTesterQty == 0){
freeTesterQty="";
}
$("#testerFree").val(freeTesterQty);
}
} else if ($(this).attr('class') == 'tester') {
testerPrice = 0;
var qty = 0;
$(".tester").each(function(){
var currClassQty = $(this).val();
if(currClassQty == ""){
currClassQty = 0;
}
qty += parseInt(currClassQty);
testerPrice += $(this).attr('data-price') * currClassQty;
});
/*var qty = $(this).val();
var freeTesterQty = $("#testerFree").val();
if (freeTesterQty === "") {
freeTesterQty = parseInt(0);
}
testerPrice = $(this).attr('data-price') * qty;*/
}
sum = retailPrice+testerPrice;
$(".sum").html(sum + "€")
});