【问题标题】:Using Fancybox with jquery-validate - confirmation box使用带有 jquery-validate 的 Fancybox - 确认框
【发布时间】:2018-06-28 05:10:30
【问题描述】:

我真的很挣扎。

我要为我的雇主做的是提交一份经过验证的表格,问题是基于提交的字段一个弹出式表格(使用 fancybox 版本 3 - http://fancyapps.com/fancybox/3/)条件是当地点是苏格兰时最终金额超过 4,550 英镑(第二个选择框中的选项 2)。

我使用$(this).validate () 运行验证(因为页面上有多个表单)。然后我想控制submitHandler,并根据条件运行一个fancybox作为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();
    }
  }
});
}

【问题讨论】:

  • 你能创建jsfiddle吗?
  • 是的,我会在一小时内完成一个
  • 你能在那里做ajax post吗?否则调用$(form).submit();发帖会出现“递归过多”的错误
  • 请更好地解释您要解决的实际问题。
  • 感谢 cmets 家伙 JsFiddle - jsfiddle.net/Callistusa/p8Lc42go essentaill 如果字段位置是 Sotland 并且字段 debtAmount 不等于第一个可选选项,则在单击表单上的提交按钮时,然后检查是否子字段 livingStatus 不等于房主,然后其他两个选定的字段收入状态等于福利和资产不等于是,然后显示一个弹出窗口,显示 GDPR 声明,我们将在其他地方引用详细信息。

标签: jquery jquery-validate fancybox-3


【解决方案1】:

试试下面,

  1. 我添加了一个隐藏字段来识别用户是否点击了“确定没问题”。

&lt;input type="hidden" value="0" name="is_validated" id="is_validated"&gt;

  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,然后向此按钮添加点击事件并相应地更改is_validated 的值。如果用户点击了Sure that is ok按钮,则将is_validated的值设置为1并再次触发表单提交点击。

只有在(location == 'Scotland' &amp;&amp; $('#is_validated').val() != '1')时才会显示花式框

【讨论】:

  • 感谢您抽出宝贵的时间帮我看这个,由于某种原因,当我运行它时,fancybox 框没有出现,已将修改上传到ivapractice.co.uk(登陆页面表单 - 第一个)
  • 似乎存在多个具有相同 id 和 name 的元素。请使其唯一并检查。
  • 我已经正常命名了,如果相同的值,是不是错了,只是通常使定位更容易,你认为这是造成冲突的原因吗
  • 所以我已经使所有输入的 id 和名称都不同,而且页面上有两种形式,我也检查过,所以我的输入上的名称不能和 id 冲突保持不变,以便在输入时可以在下一页上提交并被我的 PHP mehtod 捕获。可悲的是,花哨的盒子也没有在那个时间运行,更新的代码在同一个 url 上
  • 我发现这个问题是 JQuery Validate 运行无论文档准备好。所以我需要做的就是在 JQuery Validate 之外添加一个 onclick 函数来运行我的脚本,我将在一天左右发布一个小提琴作为答案,不得不花一些时间阅读 JQuery Validate 的文档。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多