javascript - 在号召性用语之前显示模式引导

标签 javascript php jquery bootstrap-modal

我的 HTML 代码是这样的:

<form action="form2_action.php" method="post">
    <table>
        <tr>
            <td>First Name </td>
            <td>:</td>
            <td><input type="text" id="first_name" name="first_name" required></td>
        </tr>
        <tr>
            <td>Last Name</td>
            <td>:</td>
            <td><input type="text" id="last_name" name="last_name" required></td>
        </tr>
        <tr>
            <td>Age</td>
            <td>:</td>
            <td><input type="text" id="age" name="age" required></td>
        </tr>
        <tr>
            <td></td>
            <td></td>
            <td><input type="submit" value="Submit" id="submit"></td>
        </tr>
    </table>
</form> 


<div class="modal fade" id="confirm-save" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">

            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel"><b>Are you sure to book this tour?</b></h4>
            </div>

            <div class="modal-body">
                <table style="width:100%">
                    <tr>
                        <td style="width:30%">First Name</td>
                        <td height top="40" style="width:70%" id="first_name_modal"></td>
                    </tr>
                    <tr>
                        <td>Last Name</td>
                        <td height="40" id="last_name_modal"></td>
                    </tr>
                    <tr>
                        <td>Age</td>
                        <td height="40" id="age_modal"></td>
                    </tr>                 
                </table>        

                <p class="debug-url"></p>
            </div>

            <div class="modal-footer">
                <button type="submit" class="btn btn-primary">Save changes</button>&nbsp;
                <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>  
            </div>
        </div>
    </div>
</div>

我的Javascript代码是这样的:

<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script type="text/javascript" src="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.1.0/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.1.0/css/bootstrap.min.css">

<script type="text/javascript">
    $(document).on("click", "#submit", function () {
        var first_name_modal = document.getElementById("first_name").value;
        var last_name_modal = document.getElementById("last_name").value;
        var age_modal = document.getElementById("age").value;

        if(first_name_modal!="" && last_name_modal!="" && age_modal!=""){
            $("#confirm-save").modal("show");
            $('#first_name_modal').text(first_name_modal);
            $('#last_name_modal').text(last_name_modal);
            $('#age_modal').text(age_modal);
        }
    });

</script>

演示是这样的:http://jsfiddle.net/oscar11/wjo8yjhb/

当我输入数据并点击提交按钮时,系统直接调用action form2_action.php。我希望在系统调用执行操作之前,系统首先显示模式引导。

那么,如何在号召性用语之前显示模式引导?

非常感谢

最佳答案

表单操作始终优先,您需要使用 te PreventDefaul 函数来避免这种情况,如下所示:

$(document).on("click", "#submit", function (e) {
        e.preventDefault();
        var first_name_modal = document.getElementById("first_name").value;
        var last_name_modal = document.getElementById("last_name").value;
        var age_modal = document.getElementById("age").value;

        if(first_name_modal!="" && last_name_modal!="" && age_modal!=""){
            $("#confirm-save").modal("show");
            $('#first_name_modal').text(first_name_modal);
            $('#last_name_modal').text(last_name_modal);
            $('#age_modal').text(age_modal);
        }
    });

我还更新了你的 fiddle ,所以这对你来说效果很好!祝你有美好的一天。

关于javascript - 在号召性用语之前显示模式引导,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34258197/

相关文章:

javascript - 如何删除模态打开时自动添加的额外水平滚动条?

javascript - 带有node-mysql输出到数组的异步系列

php - mysql查询日志中的^M字符,它是从哪里来的?

javascript - 服务器端验证内联显示成功或错误消息

javascript - 正则表达式 Javascript 按前两个字符匹配字符串

php - 从 preg_replace 到 preg_replace_callback

javascript - 执行功能但停止页面提交

javascript - 为什么 jquery offsetParent() 返回 html 而 native javascript 返回 body

javascript - 为什么我的 php 函数没有被 jquery $.get 执行

javascript - 如何在 Google Apps 脚本 Google 文档扩展中包含大型外部样式表和脚本