表格提交验证通过fancybox模式& jQuery的

时间:2018-02-16 11:17:59

标签: jquery forms validation fancybox

我想要一个通用的表单验证,打开一个fancybox模式弹出窗口,询问是或否。即使有多种形式,我希望它能够正常工作。

为什么表单未提交?

HTML - 表格:

<form method="post" action="http://192.168.0.4/jojo_cms/content/upload-delete" class="submit-confirm" id='upload-delete-form-1'>
    <input type="hidden" value="enrouleur-motorise-vektor-1-pour-couverture-a-barre-1-l.jpg" name="filename">
    <button name="submit" class="link" type="submit" value="submit">Delete</button>
</form>

HTML - 模态弹出窗口:

<div id="fancybox-confirm" style="display:none;">
    <p>Sure ?</p>
    <p class="tar">
        <button type="button" class="btn"  id="fancybox-confirm-no" >No</button>
        <button type="button" class="btn btn-primary"  id="fancybox-confirm-yes" >Yes</button>
    </p>
</div>

jQuery:

//creating a 'confirmed' attr = 0 for the forms and defining submit handler
$('form.submit-confirm[id]').attr('confirmed','0').submit(function(e){      

    if($(this).attr('confirmed') == '0') //the form is not confirmed yet
    {
        //setting the target-form attr of the 'Yes' button
        $('#fancybox-confirm-yes').attr('target-form',$(this).attr('id'));          

        //opening the fancybox
        $.fancybox.open({
            src  : '#fancybox-confirm',
            type : 'inline'
        });
        return false;
    }
    else
    {
            console.log('valid!'); //shown in the console when I click 'Yes'
            return; //validating the submission, not working...
    }

});

//The Yes button handler
$('#fancybox-confirm-yes').click(function(){
    if(!!$(this).attr('target-form'))
    {
        formSelector = '#' + $(this).attr('target-form');
        $(formSelector).attr('confirmed','1').submit();
    }
});

谢谢!

2 个答案:

答案 0 :(得分:0)

我建议简化逻辑,如下:

创建具有简单按钮的表单(而不是submit)。

<form method="post" action="" class="submit-confirm" id='upload-delete-form-1'>
    <input type="hidden" value="1.jpg" name="filename">
    <button name="delete" class="link" type="button">Delete</button>
</form>

相同的模态

<div id="fancybox-confirm" style="display:none;">
  <p>Sure ?</p>
  <p class="tar">
    <button type="button" class="btn" id="fancybox-confirm-no" data-fancybox-close>No</button>
    <button type="button" class="btn btn-primary" id="fancybox-confirm-yes">Yes</button>
  </p>
</div>

JS

$('form.submit-confirm[id] button.link').on('click', function(e) {
  $("#fancybox-confirm-yes").data('form-id', $(this).parent().attr('id'));

  $.fancybox.open({
    src  : '#fancybox-confirm',
    type : 'inline'
  });
});

$("#fancybox-confirm-yes").on('click', function() {
  $("#" +  $(this).data('form-id')).submit();
});

演示 - https://codepen.io/anon/pen/xYPypO?editors=1010

答案 1 :(得分:0)

解决了!提交按钮名称&#34;提交&#34;被干扰了!