【发布时间】:2017-09-22 14:46:18
【问题描述】:
在 jQuery 中处理表单验证。有三种付款方式(信用卡、比特币、贝宝)。当用户选择信用卡以外的任何内容并提交时,表单会触发对信用卡验证的调用,即使我已经添加了语言以在选择不是信用卡时停止。它还将价值重置为信用卡。
我确实有根据付款选择隐藏或显示字段的代码
//Changes Payment Sections Based on User Payment Choice
$('#bitcoin').hide();
$('#paypal').hide();
$("#payment option[value='credit card']").prop('selected', true);
$('#payment').on('change', function(){
if( $(this).val() == 'credit card' ){
$('#bitcoin').hide();
$('#paypal').hide();
$('#credit-card').show();
} else if ( $(this).val()=='paypal' ){
$('#paypal').show();
$('#credit-card').hide();
$('#bitcoin').hide();
} else if ( $(this).val()=='bitcoin'){
$('#paypal').hide();
$('#credit-card').hide();
$('#bitcoin').show();
}
});
这就是表单提交时发生的事情
// On Form Submission Validate Form
$("#contact_submit button").click(function(event){
error_name = validateName();
error_email = validateEmail();
error_activity = validateActivities();
isCreditIssue = validateCredit();
var valid = true;
if ((error_name) || (error_email) || (error_activity) || (isCreditIssue)){
console.log("errors");
valid = false;
event.preventDefault();
} else {
alert('GREAT! form completed');
valid = true;
}
if (valid) {
return;
}
});
这里是 validateCredit 函数。我的预感可能是嵌套的如果我在这里可能无法正常工作。我试图改变这一点,但无法让任何东西正常工作。
//Check for Credit Card Issue -- Any problems with CC, Zip Code or CVV
function validateCredit(){
var credit = $("#payment option[value='credit card']");
var paypal = $("#payment option[value='paypal']");
var bitcoin = $("#payment option[value='bitcoin']");
isCreditIssue = false;
if (credit.prop('selected', true)){
errorCC = validateCC();
errorZip = validateZip();
errorCVV = validateCVV();
if ((errorCC) || (errorZip) || (errorCVV)){
isCreditIssue = true;
console.log('credit issue');
} }
else if (bitcoin.prop('selected', true)){
console.log('bitcoin');
isCreditIssue = false;
} else if (paypal.prop('selected', true)){
console.log('paypal');
isCreditIssue = false;
}
return isCreditIssue;
}
【问题讨论】:
标签: javascript jquery