0

我有一个带有焦点事件的表单文本框。当文本框失去焦点时,会弹出一个确认框,询问“确定”或“取消”。

如果用户单击确定,则表单提交。如何做到这一点?

我试过这个,但没有运气:

//<![CDATA[ 
$(function(){
$('#item_new').focusout(function() {
confirm("add this item?");

        'Confirm submit': function() {
            currentForm.submit();
        },
        Cancel: function() {
            $(this).dialog('close');
        }

});
});
//]]> 

请指教。

4

1 回答 1

0

当你分解你的代码时,你会发现确认和休息根本没有联系。

这是一些可以提供帮助的代码:

$('#item_new').focusout(function(e){

    // ask the user and keep his/her choice
    var ok = window.confirm('add this item?');

    // here, the 'ok' variable will be 'true' if OK was clicked,
    // false otherwise
    if( ok ) {
       // do the submit action.
       // replace '<id_of_the_form_to_submit>' with the id of the form
       $('#<id_of_the_form_to_submit>').submit();
    }
});

看到这个jsfiddle:http: //jsfiddle.net/arnaudj/c6z44mr9/

编辑:请注意,该window.confirm调用将弹出一个基本的特定于浏览器的确认弹出窗口,并且您不能根据需要使用样式“装饰”。

于 2015-01-22T22:51:40.460 回答