如何设置一次优惠券代码是否使用过一次的检查,然后不允许一次使用第二次 [英] How to set a check if coupon code used one time then do not allow to use it second time in one transection
本文介绍了如何设置一次优惠券代码是否使用过一次的检查,然后不允许一次使用第二次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码 html输入字段
<input type="text" size = "3" name="couponadd" id="couponadd"
oninput="myFunction()" class="field" placeholder="Enter Coupon Code" />
脚本代码
<script>
var lastval;
function myFunction() {
从数据库中获取CouponDC,TicketypDC和CouponPrcDC
var CouponDC = $('#dbcoupan').val();
var TicketypDC = $('#dbtckettype').val();
var CouponPrcDC = $('#dbprice').val();
var total_price = $('#total_price').val();
从输入中获取优惠券
var getcoupon = $("#couponadd").val(),
txt='Invaild Coupon';
检查用户是否输入相同的优惠券
if(getcoupon == lastval )
{
alert('You Cant Enter Same Code Again');
}
如果优惠券代码与数据库优惠券匹配
else if (getcoupon == CouponDC ) {
$amount=CouponPrcDC;
total_price = total_price * ((100-$amount) / 100);
如果有匹配项,则减去总数的一部分
total_price = Math.round(total_price);
document.getElementById('Voucher_value').value = total_price;
}
如果coupo与数据库优惠券不匹配
else if(getcoupon != CouponDC && getcoupon.length ==5 )
{
alert('WRONG COUPON CODE');
}
**store last value enter in input**
lastval = getcoupon;
$('#total_price').val(total_price);
}
</script>
推荐答案
您可以将其存储在数组中,并在继续操作之前检查它是否存在.
You can store it in an array and check if it exists before moving ahead.
下面的伪代码:
var couponArr = [];
var getcoupon = $("#couponadd").val();
if($.inArray(getcoupon, couponArr) !== -1) {
alert('Coupon already used, can\'t use again.');
} else {
couponArr.push(getcoupon);
// your code here..
}
inArray
返回数组中元素的索引,而不是指示该项目是否存在于数组中的布尔值.如果找不到该元素,将返回-1
.
inArray
returns the index of the element in the array, not a boolean indicating if the item exists in the array. If the element was not found, -1
will be returned.
这篇关于如何设置一次优惠券代码是否使用过一次的检查,然后不允许一次使用第二次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文