jQuery validate Validate radio Instance

  • 2021-07-24 10:11:48
  • OfStack

The specific code is as follows:


<div class="row cl"> 
  <label class="form-label col-xs-4 col-sm-3"><span class="c-red">*</span> Coupon type: </label> 
  <div class="formControls col-xs-8 col-sm-9 skin-minimal"> 
    <div class="radio-box"> 
      <input name="couponType" type="radio" value="1" id="couponType_1" <#if couponInfo.couponType == 1>checked</#if>> 
      <label for="couponType_1"> Full discount coupon </label> 
    </div> 
    <div class="radio-box"> 
      <input type="radio" name="couponType" value="2" id="couponType_2" <#if couponInfo.couponType == 2>checked</#if>> 
      <label for="couponType_2"> Discount coupon </label> 
    </div> 
    <div class="radio-box"> 
      <input type="radio" name="couponType" value="3" id="couponType_3" <#if couponInfo.couponType == 3>checked</#if>> 
      <label for="couponType_3"> Cash coupon </label> 
    </div> 
  <label id="couponType-error" for="couponType" class="error"></label> 
  </div> 
</div> 
<div class="row cl" id="couponMax_div"> 
  <label class="form-label col-xs-4 col-sm-3"> Maximum amount of discount: </label> 
  <div class="formControls col-xs-8 col-sm-9"> 
    <input type="text" class="input-text" placeholder=" Only fill in the case of discount coupons " name="couponMax" id="couponMax" value="${couponInfo.couponMax}"> 
  </div> 
</div> 

Among them, the custom error prompt location only needs to be modified <label id="couponType-error" for="couponType" class="error"></label> The contents of id and for can be viewed in the browser F12.


$("#form-member-add").validate({ 
  rules:{ 
    sendCouponType:{ 
      required:true 
    }, 
    platformId:{ 
      required:true 
    }, 
    couponType:{ 
      required:true 
    } 
  }, 
  onkeyup:false, 
  focusCleanup:true, 
  success:"valid", 
  submitHandler:function(form){ 
    var couponInfo = getCouponParameter(); 
    saveCouponInfo(couponInfo); 
  } 
}); 
function saveCouponInfo(couponInfo){ 
  $.ajax({ 
    type:'POST', 
    url: "/coupon/save", 
    data: { 
      couponStr:couponInfo 
    }, 
    async: false, 
    success: function(data){ 
      if(data.code == 200){ 
        var index = parent.layer.getFrameIndex(window.name); 
        parent.$('#btn_search').click(); 
        parent.layer.close(index); 
      }else layer.alert(" Operation failed "); 
    } 
  });  
} 
//  Get the coupon form value  
var getCouponParameter = function(){ 
  var couponInfo = {}; 
  couponInfo.id = couponId; 
  // Coupon Activity Name  
  couponInfo.couponName = $('#couponName').val(); 
  // Scope of use  
  couponInfo.couponUsingRange = $("input[name='couponUsingRange']:checked").val(); 
  // Distribution type  
  couponInfo.sendCouponType = $("input[name='sendCouponType']:checked").val(); 
  // Use the platform  
  couponInfo.platformId = $("input[name='platformId']:checked").val(); 
  // Coupon type  
  couponInfo.couponType = $("input[name='couponType']:checked").val(); 
  // Restrictions on coupon use  
  couponInfo.meetPrice = $('#meetPrice').val(); 
  // Coupon denomination  
  couponInfo.couponAmount = $('#couponAmount').val(); 
  // Activity start time  
  couponInfo.couponStartDate = $('#couponStartDate').val(); 
  // End time of activity  
  couponInfo.couponEndDate = $('#couponEndDate').val(); 
  // Quantity of coupons  
  couponInfo.couponNum = $('#couponNum').val(); 
  // Limit on the number of coupons users receive  
  couponInfo.limitGetNum = $('#limitGetNum').val(); 
  // Remarks  
  couponInfo.remark = $('#remark').val(); 
  return JSON.stringify(couponInfo); 
} 
$("input:radio[name='couponType']").click(function(){ 
  //1. Full discount coupon    2. Discount coupon     3. Cash coupon  
  var tt = $(this).val(); 
  switch(tt){ 
    case '1' : 
      $('#couponMax_div').hide(); 
      break; 
    case '2' : 
      $('#couponMax_div').show(); 
      break; 
    case '3' : 
      $('#couponMax_div').hide(); 
      break; 
    default: 
      break; 
  } 
}); 

The above is to verify and pass parameters to the background, and to listen for events for radio binding.


Related articles: