【问题标题】:Alertify confirms and submits a form automatically without clicking OKAlertify 自动确认并提交表单,无需单击“确定”
【发布时间】:2016-03-06 04:28:21
【问题描述】:

我正在使用引导程序创建一个博客,并且我有一个提交类别的表单:

<form action="categories.php" id="category-form" class="form-horizontal" role="form" method="post">
  <div class="form-group">
    <label for="category" class="col-sm-2 control-label">Category</label>
    <div class="col-sm-10">
      <input type="text" name="category" class="form-control" id="category" placeholder="Category">
    </div>
  </div>
  <div class="form-group">
    <div class="col-sm-offset-2 col-sm-10">
      <input type="submit" name="submit" id="btn-submit" class="btn btn-primary" value="Add Category">
    </div>
  </div>
</form>

当我按下表单按钮“添加类别”时,会出现对话框,但几秒钟后它会立即提交,并且对话框会消失,而无需单击“是”或“否”按钮,在网上搜索我找到了一些解决方案,但没有不适合我。我用于 Alertify JS 的代码如下:

$("#btn-submit").on("click", function(){
    alertify.confirm("This is an alert dialog?", function(e){
        if (e) {
            alertify.success("Category was saved.")
        } else {
            alertify.error("Category not saved.");
        }
    });
return false;
});

我也试试event.preventDefault();:

$("#btn-submit").on("click", function(event){
    event.preventDefault();
    alertify.confirm("This is an alert dialog?", function(e){
        if (e) {
            $("#category-form").submit();
            alertify.success("Category was saved.")
            return true;
        } else {
            alertify.error("Category not saved.");
            return false;
        }
    });
});

但效果不佳。任何帮助请...谢谢。

【问题讨论】:

    标签: javascript jquery alertify


    【解决方案1】:
         Ok, This was posted and answered a long time ago. But as I was developing asp net 
        core in 2020, I came across AlertifyJS. I like the library. A solution for this 
        issue is:
        1) I used <button>
        2) I used e.preventdefault()
        3) I used ('#category-form').submit() 
        4) Make sure you add id='category-form' in your form and also and id for button. 
        5) Use button and not input type=submit. 
         There could be a difference in how they work. 
         The difference is that <button> can have content, 
         whereas <input> cannot (it is a null element).
    
            $('#btn-submit').on('click', function (e) {
                e.preventDefault();
                alertify.confirm('Save Category', 'Do you want to proceed?', function (e) 
                {
                    if (e) {
                        $("#category-form").submit();
                        alertify.success('Category Saved Successfully.');
                        return true;
                    }
                    else {
                        alertify.error("Category was not saved.");
                        return false;
                    }                   
                }, function () { alertify.error('Cancel') });
            });
    

    【讨论】:

    • 另一个有趣的选项,特别是在 ASP net core Razor 页面中不触发页面后面的方法是代码如下:$('#btn-submit').on('click', function ( ) { if (confirm('Do you want to continue?')) { alert('Thanks for confirming'); return true; } else { alert('为什么要按取消?你应该已经确认了'); return false ; } });
    【解决方案2】:

    alertify 在确认函数中基本上采用 4 个参数

    alertify.confirm(title, message, onconfirm, oncancel);
    

    你不必做if(e)这个代码

    $("#formID").on("click", function(){
        alertify.confirm("This is an alert dialog?", function(e){
            if (e) {
                alertify.success("Category was saved.")
            } else {
                alertify.error("Category not saved.");
            }
        });
    return false;
    });
    

    现在会变成

    $("#formID").submit(function(e){
      e.preventDefault();
     alertify.confirm('Confirm Title', 'Confirm Message', function(){
       // call the function that will handle your form submission may be ajax don't write $('#fomr').submit() becasue it will start an endless loop through this function   
      }, function(){ 
        // when user click cancel
      });
    
      });
    

    【讨论】:

      【解决方案3】:

      你能不能试试下面的代码:

      您需要从您的button 中删除type="submit"。否则默认提交表单。

      HTML:

      <form action="categories.php" id="category-form" class="form-horizontal" role="form" method="post">
        <div class="form-group">
          <label for="category" class="col-sm-2 control-label">Category</label>
          <div class="col-sm-10">
            <input type="text" name="category" class="form-control" id="category" placeholder="Category">
          </div>
        </div>
        <div class="form-group">
          <div class="col-sm-offset-2 col-sm-10">
            <input type="button" id="btn-submit" class="btn btn-primary" value="Add Category">
          </div>
        </div>
      </form>
      

      JQuery 代码:

      //your button click code
      $("#btn-submit").on("click", function(event){
          event.preventDefault();
          alertify.confirm("This is an alert dialog?", function(e){
              if (e) {
                  $("#category-form").submit();
                  alertify.success("Category was saved.")
                  return true;
              } else {
                  alertify.error("Category not saved.");
                  return false;
              }
          });
      });
      

      【讨论】:

      • 感谢@vijayP 的快速回复,是的,第一部分是避免提交,但由于某种原因,当返回 false 时,表单没有保存数据。我有点困惑。
      • 哦..这是正确的。将在一段时间内更新答案。
      • 感谢您的帮助,我尝试了您的代码但不起作用,显然表单提交但没有数据,我使用 AJAX 和 PHP 保存数据但也不起作用,感谢您的帮助@ vijayP
      • 我开始认为 Alertify JS 插件有问题。我可能会使用 JavaScript 自然确认对话框 =D
      【解决方案4】:

      试试这样的。我认为您希望在提交表单之前进行确认,因此添加了确认消息部分。希望它会有所帮助。

      $("#category-form").submit(function (e) {
                  var result = confirm("Are you sure ?");
                  if(result){
                      // do something
                  } else {
                      alertify.error("Error mesage.");
                      e.preventDefault(); // this will prevent from submitting the form.
                      }
              });
      

      【讨论】:

      • 感谢您的回复@vitally,我尝试了您的代码,显然它运行良好,确认对话框中的唯一问题是没有执行 Alertify JS 插件,但感谢您的回答,如果您有另一个解决方案是非常受欢迎的。 =)
      • 尽量避免alertify。使用默认警报框,或者如果您使用的是引导框架,请尝试使用模式框。
      • 谢谢@vitally,我认为这是最好的选择
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-23
      • 1970-01-01
      • 2017-10-02
      • 2019-03-12
      • 1970-01-01
      相关资源
      最近更新 更多