我试图根据信用卡验证是否返回 true 来隐藏/显示表单上的按钮。因此,如果是真的,他们可以点击结账,否则就不能。
<script>
$(function test() {
$('#cardNo').validateCreditCard(function (result) {
$('.log').html('Card type: ' + (result.card_type == null ? '-' : result.card_type.name)
+ '<br>Valid: ' + result.valid
+ '<br>Length valid: ' + result.length_valid
+ '<br>Luhn valid: ' + result.luhn_valid);
console.log(result.valid);
console.log(result.length_valid);
console.log(result.luhn_valid);
if (result.valid == true && result.length_valid == true && result.luhn_valid == true) {
console.log('we have entered the if statement');
document.getElementById("checkoutButton").style.dislay = 'block';
}
else {
console.log('we are not in the if statement');
document.getElementById("checkoutButton").style.display = 'none';
}
});
});
</script>
我已经验证,当我输入正确的信用卡号时,我们确实会进入 if 语句。控制台将显示“我们已输入 if 语句”。但是,结账按钮不会在页面上可见。
我想知道是否可以在 javascript 中做这样的事情?感谢您的帮助!
最佳答案
只需将您的代码替换为以下代码即可:
if (result.valid == true && result.length_valid == true && result.luhn_valid == true) {
console.log('we have entered the if statement');
document.getElementById("checkoutButton").style.display = 'block';
}
else {
console.log('we are not in the if statement');
document.getElementById("checkoutButton").style.display = 'none';
}
就是这样,享受编码吧:)
关于JavaScript 隐藏/显示按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40055445/