【问题标题】:jQuery dialog box for Form submit Or not表单提交或不提交的jQuery对话框
【发布时间】:2017-03-22 09:34:45
【问题描述】:

我正在使用jQuery对话框进行确认,无论是Submit表单还是Not
在提交表单之前我正在做一些验证。当我单击提交按钮时,它首先提醒 End,它位于提交按钮单击处理程序的末尾,并且我的验证失败
以下是我的代码 sn-p

$('#id-submit-btn').click( function( event ) {
    //var $this = $(this);
    var submit = true;
    if( !$('.class-employees-cb:checked').length ) {
        $("<div title='Employee Selection'>Please select at least one employee.</div>").dialog({
            buttons: [ { text: 'OK', click: function() { $(this).dialog('close'); } } ]
        });
        submit = false;
        //event.preventDefault();
        //alert("2");
    }
    if( $('input:checkbox[name=isPrepondPostpond]').is(':checked') ) {
        //event.preventDefault();
        $("<div title='Prepond/Postpond Alert'>Have You Changed Meeting Date before Prepond / Postpond ?</div>").dialog({
            buttons:[
                {
                    text: 'Yes',
                    click: function() {
                        $(this).dialog('close');
                        $('#id-submit-btn').submit();
                    }
                },
                {
                    text: 'No',
                    click: function() {
                        submit = false;
                        //event.preventDefault();
                        $(this).dialog('close');
                    }
                }
            ]
        });
        //alert("5");
    }
    alert("End");
    //event.preventDefault();
    if( !submit )
        return false;
});

我想阻止表单提交如果用户点击否,如何实现?

【问题讨论】:

  • 请使用&lt;&gt; 按钮创建minimal reproducible examplereturn submit 也足够了。但是,我强烈建议使用表单的提交事件并在出现错误时使用 preventDefault。也不要在表单中调用任何“提交”
  • 我会尽力让你知道
  • 我把$('#id-submit-btn').click(function( event ) { 换成了$('#id-submit-form').submit( function( event ) { ,但是它不等我点击对话框的按钮,直接提交表单,如果验证失败也取消注释event.preventDefault();
  • $('#id-submit-form').on("submit", function( event ) {event.preventDefault(); .... })
  • 还有 `$('#id-submit-form').submit();` 在确定按钮上

标签: jquery forms jquery-ui-dialog


【解决方案1】:

试试这个

$('#id-submit-form').on("submit", function(event) {
  event.preventDefault(); // cancel the submission
  if (!$('.class-employees-cb:checked').length) {
    $("<div title='Employee Selection'>Please select at least one employee.</div>").dialog({
      buttons: [{
        text: 'OK',
        click: function() {
          $(this).dialog('close');
        }
      }]
    });
  }
  if ($('input:checkbox[name=isPrepondPostpond]').is(':checked')) {
    $("<div title='Prepond/Postpond Alert'>Have You Changed Meeting Date before Prepond / Postpond ?</div>").dialog({
      buttons: [{
          text: 'Yes',
          click: function() {
            $(this).dialog('close');
            $('#id-submit-form')[0].submit(); // submit the form
          }
        },
        {
          text: 'No',
          click: function() {
            $(this).dialog('close');
          }
        }
      ]
    });
  }
});

【讨论】:

  • 我会试试这个
  • 只有在你没有name="submit"的地方才会起作用
  • 如果我使用动态对话框这样可以吗,$("&lt;div title='Employee Selection'&gt;Please select at least one employee.&lt;/div&gt;").dialog({})
  • It will only work if you do NOT have name="submit" somewhere,然后会发生什么,因为我遇到了&lt;input type='submit' name='isSave'&gt; 参数的问题isSave 没有传递
  • Name=submit 是个问题 - 任何其他名称都不是。如果你想传递一些东西,添加一个值
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-29
  • 2015-06-15
  • 1970-01-01
  • 2018-05-14
相关资源
最近更新 更多