【问题标题】:Nested If Statement in jQueryjQuery中的嵌套If语句
【发布时间】:2017-09-22 14:46:18
【问题描述】:

在 jQuery 中处理表单验证。有三种付款方式(信用卡、比特币、贝宝)。当用户选择信用卡以外的任何内容并提交时,表单会触发对信用卡验证的调用,即使我已经添加了语言以在选择不是信用卡时停止。它还将价值重置为信用卡。

here is jfiddle

我确实有根据付款选择隐藏或显示字段的代码

//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;
}

【问题讨论】:

  • 您设置的属性值没有得到一个。查看prop()的文档
  • 你说的很对...非常感谢
  • 只是一个评论...如果您将值 credit card 更改为 credit-card 您的付款更改处理程序可能会减少到:$("#bitcoin, #paypal, #credit-card").hide().filter("#" + this.value).show() (fiddle)
  • @Andreas ---啊...非常好..我喜欢,谢谢

标签: javascript jquery


【解决方案1】:

您需要通过给函数prop 提供第二个参数来返回属性的值,而不是设置它:

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'){
          errorCC = validateCC();
          errorZip = validateZip();
          errorCVV = validateCVV();

          if ((errorCC) || (errorZip) || (errorCVV)){
          isCreditIssue = true;
          console.log('credit issue');
        } }
        else if (bitcoin.prop('selected')){
          console.log('bitcoin');
          isCreditIssue = false;

        } else if (paypal.prop('selected'){
          console.log('paypal');
          isCreditIssue = false;
        }

        return isCreditIssue;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多