0

我有一个名为“purchaseorder”的复选框。如果选中,我想让所有的抄送字段都不需要。但是,我的脚本似乎不起作用。有任何想法吗?

复选框的名称是purchaseorder。其他字段是 CC 字段名称。默认情况下,抄送字段是必需的。只有当客户检查“采购订单”时,才需要他们。

有效:http : //validatious.org/

function ToggleCreditcardpayment() {
  if ($('input[type=checkbox][name=purchaseorder]').is(':checked')) {
    $('#NameOnCard').removeClass('required');
    $('select[name=CreditCardType]').removeClass('required');
    $('select[name=CardExpMonth]').removeClass('required');
    $('select[name=CardExpYear]').removeClass('required');
    $('#CardNumber').removeClass('required');
    $('#CVC').removeClass('required');

    $('#NameOnCard').attr('disabled', true);
    $('select[name=CreditCardType]').attr('disabled', true);
    $('select[name=CardExpMonth]').attr('disabled', true);
    $('select[name=CardExpYear]').attr('disabled', true);
    $('#CardNumber').attr('disabled', true);
    $('#CVC').attr('disabled', true);

    $('#customernumber').addClass('required');
    $('#customernumber').attr('disabled', false);
    $('#payment-method-account .error').removeClass('error');
    $('#payment-method-account ul').remove();
    $('#payment-method-cc .error').removeClass('error');
    $('#payment-method-cc ul').remove();
    ResetValidatious()

  } else {
    $('#NameOnCard').addClass('required');
    $('select[name=CreditCardType]').addClass('required');
    $('select[name=CardExpMonth]').addClass('required');
    $('select[name=CardExpYear]').addClass('required');
    $('#CardNumber').addClass('required');
    $('#CVC').addClass('required');

    $('#NameOnCard').attr('disabled', false);
    $('select[name=CreditCardType]').attr('disabled', false);
    $('select[name=CardExpMonth]').attr('disabled', false);
    $('select[name=CardExpYear]').attr('disabled', false);
    $('#CardNumber').attr('disabled', false);
    $('#CVC').attr('disabled', false);

    $('#customernumber').removeClass('required');
    $('#customernumber').attr('disabled', true);
    $('#payment-method-account .error').removeClass('error');
    $('#payment-method-account ul').remove();
    $('#payment-method-cc .error').removeClass('error');
    $('#payment-method-cc ul').remove();
    ResetValidatious();
  }
}

验证服务器端:

    if(form["purchaseorder"].Value == "yes") 
     {
         UserFormElement elementCustomerNumber = form.GetElement("CustomerNumber");
         if (null != elementCustomerNumber && elementCustomerNumber.GetValue().Trim() == "")
         {
            elementCustomerNumber.AddError("<br/>Customer number is required for pre-approved account.");
         }
         if(form.HasError)
         {
           Redirect("checkout.aspx",true);
           return;
         }

        Payment payment = new Payment(CustomerCtxt);
        payment.PaymentTypeId = DMinSite.Common.DmiProxy.GetInterface<DMinSite.Common.Interfaces.ILookupCommonService>().GetLookupIdByValue(context, DMinSite.Common.Constants.LookupTypeEnum.PaymentType, "PO");
        PaymentPurchaseOrder paymentPurchaseOrder = new PaymentPurchaseOrder(context);
        paymentPurchaseOrder.Amount = cart.CartTotal;
        paymentPurchaseOrder.CustomerNumber = elementCustomerNumber.GetValue().Trim();
        payment.PaymentInfo = paymentPurchaseOrder;

        cart.AddPayment(payment);
    }
    else
    {
        CreditCard card = new CreditCard(CustomerCtxt);
        String note =  form.GetElement("Special").GetValue();
        note = note.Replace('"','\"');
        //order level note
        cart.Note = note;

        UserFormElement elementCCTypeId = form.GetElement("CreditCardType");
        card.CreditCardTypeId = elementCCTypeId.GetValue<int>();

        UserFormElement elementCCNumber = form.GetElement("CardNumber");
        card.Number = elementCCNumber.GetValue();

        UserFormElement elementCCName = form.GetElement("NameOnCard");
        card.NameOnCard = elementCCName.GetValue();

        UserFormElement elementCCExpYear = form.GetElement("CardExpYear");
        UserFormElement elementCCExpMonth = form.GetElement("CardExpMonth");
        if (elementCCExpMonth.GetValue().Trim() == "" || elementCCExpYear.GetValue().Trim() == "")
        {
            elementCCExpMonth.AddError("<br/>The expiration month and/or year is not valid.  Please check your selections and try again.");
        }
        else
        {
            card.ExpirationDate = ControlUtility.ConstructDateTime(
            elementCCExpYear.GetValue<int>(), 
            elementCCExpMonth.GetValue<int>(), 1);
        }

        UserFormElement elementCCCvv2 = form.GetElement("CVC");
        card.Cvv2 = elementCCCvv2.GetValue();

        //check fields
        if (elementCCNumber.GetValue().Trim() == "" || !card.IsValidCreditCardNumber(CustomerCtxt))
        {
            elementCCNumber.AddError("<br/>The credit card number is not valid.  Please check the number and try again.");
        }
        if (elementCCExpYear.GetValue().Trim() + elementCCExpMonth.GetValue().Trim() == "" || card.IsExpired(CustomerCtxt))
        {
            elementCCExpYear.AddError("<br/>The credit card has expired.  Please check the expiration date and try again.");
        }
        if (elementCCCvv2.GetValue().Trim() == "" || !card.IsValidCvv2(CustomerCtxt))
        {
            elementCCCvv2.AddError("<br/>The credit card CVV2 number is not valid.  Please check the CVV2 number and try again.");
        }
        if (elementCCName.GetValue().Trim() == "")
        {
            elementCCName.AddError("<br/>The credit card name is not filled out.  Please check the name and try again.");
        }

        if(form.HasError)
        {
           Redirect("checkout.aspx",true);
           return;
        }

        Payment payment = new Payment(CustomerCtxt);
        payment.PaymentTypeId = Core.GetLookupIdByValue("PaymentType", "CreditCard");
        payment.PaymentInfo = card;
        payment.Amount = cart.CartTotal;        
        cart.AddPayment(payment);
    }
4

2 回答 2

0

试试用这个 if ($('input[id=purchaseorder]').is(':checked')) 复选框的输入,应该有和id,可以和name属性一样。

于 2013-10-09T17:40:23.530 回答
0

第一个选项

function ToggleCreditcardpayment() {
  if ($('input[type=checkbox][name=purchaseorder]').is(':checked')) {
  $('input[type=checkbox][name=purchaseorder]').val('Yes');
  //Your other code
  ResetValidatious()

  } else {
  $('input[type=checkbox][name=purchaseorder]').val('No');
  //Your other code
  ResetValidatious();
  }
}

第二个选项 创建一个隐藏字段来存储值取决于是否选中

function ToggleCreditcardpayment() {
    if ($('input[type=checkbox][name=purchaseorderhidden]').is(':checked')) {
    $('input[type=checkbox][name=purchaseorder]').val('Yes');
    //Your other code
    ResetValidatious()

    } else {
    $('input[type=checkbox][name=purchaseorderhidden]').val('No');
    //Your other code
    ResetValidatious();
    }
}

服务器端

if(form["purchaseorderhidden"].Value == "yes")
{
  //Your validation code
}
else
{
  //Your validation code
}
于 2013-10-09T18:42:41.057 回答