提交AJAX表单后创建自定义确认框

时间:2012-09-20 15:04:18

标签: php javascript jquery css ajax

我正在使用AJAX JQuery验证插件提交表单。代码看起来像这样:

$(document).ready(function(){
$("#myform").validate({
    debug: false,
    rules: {
        fname: {required: true}, 
        sname: {required: true}, 
        gender: {required: true}, 
    },
    messages: {
        fname: {required: " *"},
        sname: {required: " *"},
        gender: {required: " *"},
    },
    submitHandler: function(form) {
        $('input[type=submit]').attr('disabled', 'disabled');
        $('#results').html('Loading...');
        $.post('process_participant.php', $("#myform").serialize(), function(data) {
            $('#results').html(data);
        });
    }
});
}); 

现在问题是我将这些信息发送到我的PHP页面后进行修改(process_participant.php)我想回复一个确认框,询问用户是否想要添加另一个参与者。 PHP代码看起来像这样:

if (everything processes okay){

echo '<script type="text/javascript">';
echo 'input_box=confirm("Saved successfully. Would you like to add another participant?");
            if (input_box==true) {
                window.location = "new_participant.php"
            }   
            else {
                window.location = "home.php"
            }';
echo '</script>';  
}

一切正常,但默认确认框不可接受。我需要能够设置样式并将其从OK和CANCEL更改为YES和NO。我决定使用这个插件:http://kailashnadh.name/code/jqdialog/并在PHP端使用以下代码:

echo '<script type="text/javascript">';

echo "  function confirmBox() {
                $.jqDialog.confirm(\"Are you sure want to click either of these buttons?\",
                function() { window.location = \"new_participant.php\"; },
                function() { window.location = \"home.php\"; }  
                );
            });";

echo "confirmBox();";

然而我似乎无法让它发挥作用。也许我应该避免使用插件?整个AJAX的事情让事情变得混乱。在这种情况下,任何人都知道实现自定义确认框的最佳方法吗?

2 个答案:

答案 0 :(得分:0)

从头开始创建框:display:none
当来自post请求的函数返回
时 做检查(基于可变数据)
如果一切正常,请使用jquery的show()

更改可见性

答案 1 :(得分:0)

尝试this

易于实现,只需为警报和符合框定义自己的html

相关问题