使用jquery确认提交表单

时间:2018-01-23 10:37:54

标签: javascript jquery

我使用Jquery Confirmation插件,但如果点击“确认”按钮,我对如何提交表单感到困惑。这是我的代码:

$(function() {
  $("button[name='btn-delete-akun']").confirm({
    title: '<p style="color:red;"> <span class="glyphicon glyphicon-warning-sign" aria-hidden="true"></span>  Peringatan!<p>',
    content: 'Apakah anda yakin ingin menghapus event? ',
    useBootstrap: true,
    buttons: {
      Confirm: {
        btnClass: 'btn-red',
        //how to submit form if i click this confirmation button
      },
      Cancel: function() {}
    }
  });
});

谢谢。

2 个答案:

答案 0 :(得分:1)

我认为这个例子很有用。试试这个。

$.confirm({
    title: 'Prompt!',
    content: '' +
    '<form action="" class="formName">' +
    '<div class="form-group">' +
    '<label>Enter something here</label>' +
    '<input type="text" placeholder="Your name" class="name form-control" required />' +
    '</div>' +
    '</form>',
    buttons: {
        formSubmit: {
            text: 'Submit',
            btnClass: 'btn-blue',
            action: function () {
                var name = this.$content.find('.name').val();
                if(!name){
                    $.alert('provide a valid name');
                    return false;
                }
                $.alert('Your name is ' + name);
            }
        },
        cancel: function () {
            //close
        },
    },
    onContentReady: function () {
        // bind to events
        var jc = this;
        this.$content.find('form').on('submit', function (e) {
            // if the user submits the form by pressing enter in the field.
            e.preventDefault();
            jc.$$formSubmit.trigger('click'); // reference the button and click it
        });
    }
}); 

详见:
https://craftpip.github.io/jquery-confirm/

使用ajax方法:

$.confirm({
        title: 'Are you sure you want to approve this application?',
        content: '',
        buttons: {
            confirm: function () {

                $.ajax({
                    url: 'myUrl',
                    type: "POST",
                    data: {
                        // data stuff here
                    },
                    success: function () {
                        // does some stuff here...
                    }
                }),
            cancel: function () {

            }
        }
    }
    });

您还可以在formSubmit操作中编写ajax方法。

答案 1 :(得分:0)

你试过这个吗?

buttons: {
              Yes: function () {$('#yourForm').submit()},
              No: function () {}
            }