【问题标题】:How to make an "Edit User" form inside Bootstrap Modal using JQUERY?如何使用 JQUERY 在 Bootstrap Modal 中制作“编辑用户”表单?
【发布时间】:2014-06-15 15:03:14
【问题描述】:

首先很抱歉我的英语不好,我来自一个说西班牙语的国家。 我是 Web 开发的新手,所以我手头有一些 Web 项目,并且遇到了与此要求相关的问题。

在控制器 index() 函数中,我需要捕获一个 ID 属性,该属性通过如下链接:

echo '<td><a class="btn btn-default" role="button" data-toggle="modal" href="#edituser" data-href="admin/editar/' . $usuario->id . '"><i class="glyphicon glyphicon-edit"></i> Editar</a></td>';

所以它可以运行

$data['editar_instructor'] = $this->user->obtener_datos_por_id($id);

它可以显示我正在尝试编辑的用户数据。我已经有一个 JS 代码可以捕获 Modal 表单数据并保存到 DB。

//Wait until the DOM is fully loaded
$(document).ready(function(){
    //Listen for the form submit
    $('#edituser').submit(editUser);
});

//The function that handles the process
function editUser(event)
{
    //Stop the form from submitting
    event.preventDefault();

    //Collect our form data.
    var form_data = {
        username : $("[name='username']").val(),
        password : $("[name='password']").val(),
        repassword : $("[name='repassword']").val(),
        JCCE : $("[name='JCCE']").val(),
        fullname : $("[name='fullname']").val(),
        privilegios : $("[name='privilegios']").val()
    };
    var action = $(this).attr('data-href');
    //Begin the ajax call
    $.ajax({
        url: action,
        type: "POST",
        data: form_data,
        dataType: "json",
        cache: false,

        success: function (json) {
            if (json.error==1)
            {
                //Show the user the errors.
                $('#EditUserError').html(json.message);
            } else {
                //Hide our form
                $('#edituserform').slideUp();
                //Show the success message
                $('#EditUserError').html(json.message).show();
            }
        }
    });
}

这已经很好了,但我不知道如何让 Modal 加载特定用户的数据。

有什么建议、想法、批评等...?我会感激任何事情。 提前致谢!

【问题讨论】:

    标签: javascript jquery codeigniter twitter-bootstrap modal-dialog


    【解决方案1】:

    让我向您展示我在一个项目中的一段工作代码。希望对你有帮助。

    在我看来,我有一个表格,我在其中回显如下数据:

    <table class="table table-striped table-bordered" id="store-table">
        <thead>
            <tr>
                <th>Id</th>
                <th>Program Name</th>
                <th>Content</th>
                <th>Quote</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
            <?php $i = 1; foreach($business_skills as $business_skill):  ?>
            <tr>
                <td width="5%;"><?php echo $i; ?></td>
                <td style="width:15%;"><?php echo $business_skill['name']; ?></td>
                <td style="width:45%;"><?php echo $business_skill['content']; ?></td>
                <td style="width:15%;"><?php echo $business_skill['quote']; ?></td>
                <td>
                    <button type="button" class="btn btn-primary btn-xs edit_button" 
                        data-toggle="modal" data-target="#myModal"
                        data-name="<?php echo $business_skill['name'];?>"
                        data-content="<?php echo htmlentities($business_skill['content']);?>"
                        data-quote="<?php echo $business_skill['quote'];?>"
                        data-id="<?php echo $business_skill['id']; ?>">
                        Edit
                    </button>
                    <button type="button" data-id="<?php echo $business_skill['id']; ?>" 
                        data-toggle="modal" data-target="#myModal3" class="btn btn-danger btn-xs delete_button">
                        Delete
                    </button>
                </td>
            </tr>
            <?php $i++; endforeach; ?>
        </tbody>
    </table>
    

    在我的 jQuery 中,我有以下代码:

    $(document).on( "click", '.edit_button',function(e) {
        var name = $(this).data('name');
        var id = $(this).data('id');
        var content = $(this).data('content');
        var quote = $(this).data('quote');
    
        $(".business_skill_id").val(id);
        $(".business_skill_name").val(name);
        $(".business_skill_quote").val(quote);
        tinyMCE.get('business_skill_content').setContent(content);   
    });
    

    填充数据的 Bootstrap 模式在这里:

    <!-- Modal for Edit button -->
    <div class="modal fade" id="myModal" 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">Edit Skill</h4>
                </div>
                <form method="post" action="<?php echo base_url(); ?>admin/edit_business_skill">
                    <div class="modal-body">
                        <div class="form-group">
                            <input class="form-control business_skill_id" type="hidden" name="id">
                            <input class="form-control business_skill_name" name="name" placeholder="Enter Skill" required>
                        </div>
                        <div class="form-group">
                            <input class="form-control business_skill_content" type="hidden" name="content">
                            <label for="heading">Enter program details</label>
                            <textarea id="business_skill_content"  name="content"></textarea>
                        </div>
                        <div class="form-group">
                            <input class="form-control business_skill_quote" type="hidden" name="quote">
                            <input class="form-control business_skill_quote" name="quote" placeholder="Enter Quote" required>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                        <button type="submit" class="btn btn-primary">Save changes</button>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <!-- End of Modal for Edit button -->
    

    这是不发出不必要的 AJAX 请求的最佳方式。我使用了“数据”属性和一些 jQuery 来获取模式中的值。

    希望这会有所帮助!干杯

    【讨论】:

    • 感谢@prat1kk,这对我有很大帮助...我在 JS 控制台中遇到了一些错误。它说 Emty 字符串传递给 getElementById()。每次我点击编辑按钮。所以我调整你的代码以适合我的,这就是我最终得到的 JQuery...
    • $(document).on("click", '.edit_button', function(e) { var username = $(this).data('username'); var id = $(this ).data('id'); var fullname = $(this).data('fullname'); var password = $(this).data('password'); var JCCE = $(this).data(' JCCE'); var privilegios = $(this).data('privilegios'); $(".id").val(id); $(".username").val(username); $(".fullname ").val(fullname); $(".password").val(password); $(".JCCE").val(JCCE); $(".privilegios").val(privilegios); });
    • @NexusProx 我希望你有我上面给出的格式的 HTML。基本上,数据属性将有助于获取标签。执行“检查元素”并查看值是否出现在数据字段中。此外,在 jquery 的“点击”功能中,尝试提醒值以查看您得到的结果
    • @prat1kk,谢谢,我遇到了同样的问题,你的方法很有效。
    • @prat1kk Err,你能告诉我如何将它用于select 输入吗?
    猜你喜欢
    • 1970-01-01
    • 2013-03-21
    • 1970-01-01
    • 2016-11-16
    • 2021-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-28
    相关资源
    最近更新 更多