弹框模块2(Bootstrap Modal)ajax <form包裹后button type="submit"触发

  1. 弹框模块内的submit不用绑定函数
  2. form里面的input可以直接提交,textarea加上name属性也可以提交了
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>用户列表</title>
    <link href="http://libs.baidu.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet">
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
    <script src="http://libs.baidu.com/bootstrap/3.0.3/js/bootstrap.min.js"></script>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<script>
    // 提交表单
    function delete_info(id)
    {
        if(!id)
        {
            alert('Error!');
            return false;
        }
        // var form_data = new Array();

        $.ajax(
                {
                    url: "action/user_action.php",
                    data:{"id":id, "act":"del"},
                    type: "post",
                    beforeSend:function()
                    {
                        $("#tip").html("<span style='color:blue'>正在处理...</span>");
                        return true;
                    },
                    success:function(data)
                    {
                        if(data > 0)
                        {
                            alert('操作成功');
                            $("#tip").html("<span style='color:blueviolet'>恭喜,删除成功!</span>");

                            // document.location.href='world_system_notice.php'
                             location.reload();
                        }
                        else
                        {
                            $("#tip").html("<span style='color:red'>失败,请重试</span>");
                            alert('操作失败');
                        }
                    },
                    error:function()
                    {
                        alert('请求出错');
                    },
                    complete:function()
                    {
                        // $('#tips').hide();
                    }
                });

        return false;
    }

    // 编辑表单
    function get_edit_info(user_id)
    {
        if(!user_id)
        {
            alert('Error!');
            return false;
        }
        // var form_data = new Array();

        $.ajax(
                {
                    url: "action/user_action.php",
                    data:{"user_id":user_id, "act":"get"},
                    type: "post",
                    beforeSend:function()
                    {
                        // $("#tip").html("<span style='color:blue'>正在处理...</span>");
                        return true;
                    },
                    success:function(data)
                    {
                        if(data)
                        {

                            // 解析json数据
                            var data = data;

                            var data_obj = eval("("+data+")");

                            // 赋值
                            $("#user_id").val(data_obj.user_id);

                            $("#name").val(data_obj.name);
                            $("#address").val(data_obj.address);
                            $("#remark").val(data_obj.remark);
                            $("#act").val("edit");

                            // 将input元素设置为readonly
                            $('#user_id').attr("readonly","readonly")
                           // location.reload();
                        }
                        else
                        {
                            $("#tip").html("<span style='color:red'>失败,请重试</span>");
                          //  alert('操作失败');
                        }
                    },
                    error:function()
                    {
                        alert('请求出错');
                    },
                    complete:function()
                    {
                        // $('#tips').hide();
                    }
                });

        return false;
    }

    // 提交表单
    function check_form()
    {
        var user_id = $.trim($('#user_id').val());
        var act     = $.trim($('#act').val());

        if(!user_id)
        {
            alert('用户ID不能为空!');
            return false;
        }
           var form_data = $('#form_data').serialize();

        // 异步提交数据到action/add_action.php页面
        $.ajax(
                {
                    url: "action/user_action.php",
                    data:{"form_data":form_data,"act":act},
                    type: "post",
                    beforeSend:function()
                    {
                        $("#tip").html("<span style='color:blue'>正在处理...</span>");
                        return true;
                    },
                    success:function(data)
                    {
                        if(data > 0)
                        {

                            var msg = "添加";
                            if(act == "edit") msg = "编辑";
                            $("#tip").html("<span style='color:blueviolet'>恭喜," +msg+ "成功!</span>");
                            // document.location.href='system_notice.php'
                            alert(msg + "OK!");
                            location.reload();
                        }
                        else
                        {
                            $("#tip").html("<span style='color:red'>失败,请重试</span>");
                            alert('操作失败');
                        }
                    },
                    error:function()
                    {
                        alert('请求出错');
                    },
                    complete:function()
                    {
                        $('#acting_tips').hide();
                    }
                });

        return false;
    }

    $(function () { $('#addUserModal').on('hide.bs.modal', function () {
        // 关闭时清空edit状态为add
        $("#act").val("add");
        location.reload();
    })
    });
</script>
<body>
<div class="container" style="min-width: 1200px;">

<h1>
用户列表
</h1>
    <form action="extreme_award_user_list.php" method="post" class="form">
    <table class="table table-bordered">
        <tbody>
        <tr>
            <td>用户ID:<input type="text" name="search_user_id" value="{search_user_id}"></td>
            <td>合计条件用户: <input type="text" name="search_total" value="{search_total}"></td>
            <td>   <!-- 按钮触发模态框 -->
                <button class="btn btn-primary btn-sm" data-toggle="modal"  data-target="#addUserModal">
                    添加用户
                </button>
            </td>
        </tr>
        <tr>
            <td colspan="10" style=" text-align: center; padding: 10px; border: none">
                <input type="submit" class="btn btn-default" value="搜索" />&nbsp;&nbsp;<a href="extreme_award_user_list.php">默认</a>
            </td>
        </tr>
        </tbody>
    </table>
    </form>

    总数(<b>{total_count}</b>)
<table class="table table-hover table-bordered" >
    <thead>
    <tr>
        <th>用户id</th>
        <th>用户名</th>
        <th>地址</th>
        <th>备注</th>
        <th>操作</th>
    </tr>
    </thead>
    <tbody>
    <!-- BEGIN list -->
    <tr>
        <td>{user_id}</td>
        <td>{name}</td>
        <td>{address}</td>
        <td>{remark}</td>
        <td>
            <button type="button" class="btn btn-info" data-toggle="modal" onclick="return get_edit_info({user_id})" data-target="#addUserModal">编辑</button>
            &nbsp;&nbsp;
        <button type="button" class="btn btn-danger" onclick="return delete_info({id})">删除</button>
        </td>

    </tr>

    <!-- END list -->


    </tbody>
</table>
    {page_str} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

    <!-- 模态框示例(Modal) -->
    <form method="post" action="" class="form-horizontal" role="form" id="form_data" onsubmit="return check_form()" style="margin: 20px;">
    <div class="modal fade" id="addUserModal" 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">
                        用户信息
                    </h4>
                </div>
                <div class="modal-body">
                    <form class="form-horizontal" role="form">
                        <div class="form-group">
                            <label for="user_id" class="col-sm-3 control-label">用户ID</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" id="user_id" name="user_id" value="{user_id}"
                                       placeholder="请输入用户ID">
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="lastname" class="col-sm-3 control-label">用户名</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" name="user_name" value="" id="user_name"
                                       placeholder="用户名">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="lastname" class="col-sm-3 control-label">地址</label>
                            <div class="col-sm-9">
                                <input type="text" class="form-control" name="address" value="" id="address"
                                       placeholder="地址">
                            </div>
                        </div>

                        <div class="form-group">
                            <label for="remark" class="col-sm-3 control-label">备注</label>
                            <div class="col-sm-9">
                                <textarea  class="form-control"  name="remark" value="{remark}" id="remark"
                                       placeholder="备注">

                                </textarea>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭
                    </button>
                    <button type="submit" class="btn btn-primary">
                        提交
                    </button><span id="tip"> </span>
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal -->
    </div>
    </form>
</div>
</body>
</html>
posted @ 2021-01-22 21:31  该显示昵称已被使用了  阅读(147)  评论(0编辑  收藏  举报