将Fancybox与jquery-validate结合使用-确认框

时间:2018-06-28 05:10:31

标签: jquery jquery-validate fancybox-3

我真的为此感到挣扎。

我要为雇主准备的工作是提交经过验证的表格,问题是,根据提交的字段创建一个弹出式表格(使用fancybox版本3-http://fancyapps.com/fancybox/3/),条件是是苏格兰,入场费超过4,550英镑(第二个选择框的选项2)。

我使用$(this).validate ()运行验证(因为页面上有多种形式)。然后,我想控制submitHandler,并根据条件运行一个confirmation的花式框,以便单击fb.box中出现的按钮,然后提交表单。

我觉得我没有传递一些变量或缺少重要的东西。我知道还有其他方法可以实现此目的,但是由于使用fancybox可以使用样式选项,这是首选方法,请帮忙,我已经花了2天的时间尝试这样做。

我的最后一招是从变量表单中获取变量并将其传递到fancybox中,然后从框中提交(这不是理想的方法,但可能是唯一的方法)。如果您想查看表格,请访问:

https://ivapractice.co.uk

  $('form').each(function() {  
$(this).validate({
  rules:{
    location:"required",
    debtAmount:"required",
    incomeStatus:"required",
    livingStatus:"required",
    assets:"required",
    fullname:{
      required:true,
      minlength:3
    },
    phonenumber:{
      required:true,
      minlength:11,
      maxlength:11
    },
    email:{
      required:true,
      emailvalidate:true
    },
    gridCheck:"required"
  },
  messages:{
    location:"Please select what region you live in",
    debtAmount:"Please select your approximate debt level",
    incomeStatus:"How do you recieve your income",
    livingStatus:"What's your current living status",
    assets:"Please select an option",
    fullname:"Please enter you full name",
    phonenumber:"Please enter a valid phone number",
    email:"Please enter a valid e-mail address",
    gridCheck:"We require your consent to submit your enquiry"
  },
  submitHandler: function(form) {
    // some other code
    // maybe disabling submit button
    // then
    var location = $('[name=location]').val();
    var debtAmount = $('[name=debtAmount]').val();
    var incomeStatus = $('[name=incomeStatus]').val();
    var livingStatus = $('[name=livingStatus]').val();
    var assets = $('[name=assets]').val();
    if(location == 'Scotland'){
      $.fancybox.open({
        src  : 
        '<div class="container"><div class="row"><div class="col"><h1 class="text-center">Thanks for you Enquiry</h1></div></div><div class="row"><div class="col text-center><p>Based on the information you have submitted, an IVA may not be suiteable.</p><p>There are many ways in which we can still help and we work with a panel of debt advice companies that could stilll help.</p><p>Your data is important to us and we just want to maek sure you are happy for you to refer you detaisl for a more suitable soluiotion.<p></div></div><div class="row"><div class="col-md-6 col-sm-12"><button data-value="0" data-fancybox-close class="btn">I need a bit more time</button></div><div class="col-md-6 col-sm-12"><button data-value="1" data-fancybox-close class="btn">Sure that is ok</button></div></div></div>',
        type : 'inline',
        opts : {
          afterLoad: function( instance, current, e) {
            var button = e ? e.target || e.currentTarget : null;
            var value  = button ? $(button).data('value') : 0;
            if(value == 1){
              alert():
            }
          }
        }
      })
    }
    else {
      //$(form).submit();
    }
  }
});
}

1 个答案:

答案 0 :(得分:0)

尝试下面,

  1. 我添加了一个隐藏字段,以标识用户是否单击了“确定”。

<input type="hidden" value="0" name="is_validated" id="is_validated">

  1. 我已经更新了submitHandler,如下所示。

    submitHandler: function(form) { 
    
    // some other code
    // maybe disabling submit button
    // then
    var location = $('[name=location]').val();
    var debtAmount = $('[name=debtAmount]').val();
    var incomeStatus = $('[name=incomeStatus]').val();
    var livingStatus = $('[name=livingStatus]').val();
    var assets = $('[name=assets]').val();
    if(location == 'Scotland' && $('#is_validated').val() != '1'){ // MODIFIED
      $.fancybox.open({
        src  : 
        '<div class="container"><div class="row"><div class="col"><h1 class="text-center">Thanks for you Enquiry</h1></div></div><div class="row"><div class="col text-center><p>Based on the information you have submitted, an IVA may not be suiteable.</p><p>There are many ways in which we can still help and we work with a panel of debt advice companies that could stilll help.</p><p>Your data is important to us and we just want to maek sure you are happy for you to refer you detaisl for a more suitable soluiotion.<p></div></div><div class="row"><div class="col-md-6 col-sm-12"><button data-value="0" class="btn data-fancybox-click">I need a bit more time</button></div><div class="col-md-6 col-sm-12"><button data-value="1" class="btn data-fancybox-click">Sure that is ok</button></div></div></div>', // MODIFIED
        type : 'inline',
        opts : {
          afterLoad: function( instance, current, e) {  // MODIFIED THIS FUNCTION
            $('body').find('.data-fancybox-click').off('click').on('click', function(e){
             var value = $(this).data("value");
             if(value == 1){
                $('#is_validated').val('1');
                $('#landingform button[type="submit"]').trigger('click');
                $.fancybox.close();
    
              }else{
                $.fancybox.close();
                $('#is_validated').val('0');
              }
            });
          }
        }
      })
    }
    else {
      $(form).submit();
    }
    }
    

从花式框中的两个按钮中删除了属性data-fancybox-close,并添加了类data-fancybox-click,然后向该按钮添加了click事件,并相应地更改了is_validated的值。如果用户单击按钮Sure that is ok,则将is_validated的值设置为1,并触发表单再次提交单击。

仅当(location == 'Scotland' && $('#is_validated').val() != '1')

时才会显示花式框