表格在提交前确认

seo*_*ppc 45 jquery

我正在使用一个简单的表单,我希望允许用户在表单提交之前确认...我知道使用jQuery会很容易,但我对代码有点困惑...

$(function() {
  $(".testform").submit(function() {
    $('.submitbtn').text('confirm');
  });
});
Run Code Online (Sandbox Code Playgroud)

我知道上面的代码不完整,我希望你的帮助完成它,所以它会发出警告(使用jQuery警报),说"请确认一切是否正确",并将提交按钮上的文字更改为"确认"而不是"提交".如果用户再次点击确认,则会提交表单.我希望这是有道理的.谢谢.

Nat*_*ano 111

$('#myForm').submit(function() {
    var c = confirm("Click OK to continue?");
    return c; //you can just return c because it will be true or false
});
Run Code Online (Sandbox Code Playgroud)


kei*_*kei 19

样品小提琴:http://jsfiddle.net/z68VD/

HTML:

<form id="uguu" action="http://google.ca">
    <input type="submit" value="text 1" />
</form>
Run Code Online (Sandbox Code Playgroud)

jquery:

$("#uguu").submit(function() {
    if ($("input[type='submit']").val() == "text 1") {
        alert("Please confirm if everything is correct");
        $("input[type='submit']").val("text 2");
        return false;
    }
});
Run Code Online (Sandbox Code Playgroud)


nik*_*hil 16

这就是我想要得到你想要的东西:

$(document).ready(function() {
    $(".testform").click(function(event) {
        if( !confirm('Are you sure that you want to submit the form') ) 
            event.preventDefault();
    });
});
Run Code Online (Sandbox Code Playgroud)

关于代码如何工作的轻微解释,当用户单击按钮然后启动确认对话框,以防用户选择没有提交表单的默认操作不执行.确认后,控制权将传递给浏览器,继续提交表格.我们在这里使用标准JavaScript确认.


小智 9

HTML

<input type="submit" id="submit" name="submit" value="save" />
Run Code Online (Sandbox Code Playgroud)

JQUERY

$(document).ready(function() {
    $("#submit").click(function(event) {
        if( !confirm('Are you sure that you want to submit the form') ){
            event.preventDefault();
        } 

    });
});
Run Code Online (Sandbox Code Playgroud)


Nik*_*ksD 7

只是...

  $('#myForm').submit(function() {
   return confirm("Click OK to continue?");
  });
Run Code Online (Sandbox Code Playgroud)

要么

  $('#myForm').submit(function() {
   var status = confirm("Click OK to continue?");
   if(status == false){
   return false;
   }
   else{
   return true; 
   }
  });
Run Code Online (Sandbox Code Playgroud)


Hru*_*shi 6

<body> 
    <form method="post">
        name<input type="text" name="text">
        <input type="submit" value="submit" onclick="return confirm('Are you sure you want to Save?')">
    </form>
</body>
Run Code Online (Sandbox Code Playgroud)


小智 5

基于easy-confirm-plugin,我做到了:

(function($) {
    $.postconfirm = {};
    $.postconfirm.locales = {};
    $.postconfirm.locales.ptBR = {
        title: 'Esta certo disto?',
        text: 'Esta certo que quer realmente ?',
        button: ['Cancela', 'Confirma'],
        closeText: 'fecha'
    };
    $.fn.postconfirm = function(options) {
        var options = jQuery.extend({
            eventType: 'click',
            icon: 'help'
        }, options);
        var locale = jQuery.extend({}, $.postconfirm.locales.ptBR, options.locale);
        var type = options.eventType;
        return this.each(function() {
            var target = this;
            var $target = jQuery(target);
            var getDlgDv = function() {
                var dlger = (options.dialog === undefined || typeof(options.dialog) != 'object');
                var dlgdv = $('<div class="dialog confirm">' + locale.text + '</div>');         
                return dlger ? dlgdv : options.dialog;          
            }           
            var dialog = getDlgDv();
            var handler = function(event) {
                    $(dialog).dialog('open');
                    event.stopImmediatePropagation();
                    event.preventDefault();
                    return false;
            };
            var init = function() 
            {
                $target.bind(type, handler); 
            };
            var buttons = {};
            buttons[locale.button[0]] = function() { $(dialog).dialog("close"); };
            buttons[locale.button[1]] = function() {
                $(dialog).dialog("close");
                alert('1');
                $target.unbind(type, handler);
                $target.click();
                $target.attr("disabled", true);
            };            
            $(dialog).dialog({
                autoOpen: false,
                resizable: false,
                draggable: true,
                closeOnEscape: true,
                width: 'auto',
                minHeight: 120,
                maxHeight: 200,
                buttons: buttons,
                title: locale.title,
                closeText: locale.closeText,
                modal: true
            });
            init();
        });
        var _attr = $.fn.attr;
        $.fn.attr = function(attr, value) {
            var returned = _attr.apply(this, arguments);
            if (attr == 'title' && returned === undefined) 
            {
                returned = '';
            }
            return returned;
        };
    };
})(jQuery);
Run Code Online (Sandbox Code Playgroud)

你只需要这样打电话:

    <script type="text/javascript">     
        $(document).ready(function () {
            $(".mybuttonselector").postconfirm({ locale: {
                        title: 'title',
                        text: 'message',
                        button: ['bt_0', 'bt_1'],
                        closeText: 'X'
                    }
                });
        });
    </script>
Run Code Online (Sandbox Code Playgroud)