在Laravel中单击关闭时,以模态清除数据

时间:2017-04-16 13:28:53

标签: php twitter-bootstrap laravel

我有一个用于创建新产品的模态(Bootstrap)。当我单击“关闭”按钮(创建新产品)时,旧数据仍然无法从模态中清除。

我的代码:

<div class="modal fade" id="product" role="dialog" aria-hidden="true" data-target="#myModal"   data-backdrop="static"  data-keyboard="false" >
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title">Create new product</h4>
            </div>
            <div class="modal-body">
                <form role="form" action="{{ route('admin.product.addProduct')}}" method="post" id="frmProduct">
                    {{ csrf_field() }}
                    <input type="hidden" name="_token" value="{{ csrf_token() }}">
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="">Product Name</label>
                                <input type="text" class="form-control" id="" name="product_name">
                            </div>
                            <div class="form-group">
                                <label for="">Product Type</label>
                                <input type="text" class="form-control" id="" name="product_type_id">
                            </div>
                            <div class="form-group">
                                <label for="">Price</label>
                                <input type="text" class="form-control" id="" name="price">
                            </div>
                            <div class="form-group">
                                <label for="">Status</label>
                                <select class="form-control input-sm m-bot15" id="" name="status">
                                    <option value="0">Inactive</option>
                                    <option value="1">Active</option>
                                </select>
                            </div>
                            <div class="form-group">
                                <label for="img">Image</label>

                                <div class="input-group">
                                    <span class="input-group-btn">
                                        <span class="btn btn-default btn-file">
                                            Choose <input type="file" id="imgInp">
                                        </span>
                                    </span>
                                    <input type="text" class="form-control" name="product_image" readonly>
                                </div>
                                <img id='img-upload' class="image_responsive" />
                            </div>
                        </div>

                    <div class="col-md-6">
                        <div class="form-group">
                            <label for="">Description</label>
                            <textarea class="form-control" id="" name="description"></textarea>
                        </div>
                        <div class="form-group">
                            <label for="">Note</label>
                            <textarea class="form-control" id="" name="addition_information"></textarea>
                        </div>

                    </div>
                </div>
            </div>
            <div class="modal-footer">
                    <input type="submit" value="Save" class="btn btn-success">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </form>
    </div>

</div>
</div>

关闭模态时如何清除数据?

2 个答案:

答案 0 :(得分:3)

您可以通过向关闭按钮添加ID来为关闭按钮添加事件监听器,例如id=close-btn

document.getElementById("close-btn").addEventListener("click", function(){ 
   document.getElementById("frmProduct").reset();
});

答案 1 :(得分:1)

您可以使用hidden.bs.modal事件:

  

当模态完成对用户的隐藏时将触发此事件(将等待CSS转换完成)​​。

摘录:

$('#product').on('hidden.bs.modal', function(e) {
    $(this).find('form').trigger('reset');
})
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>


<!-- Button trigger modal -->
<button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#product">
    Launch modal
</button>
<div class="modal fade" id="product" role="dialog" aria-hidden="true" data-target="#myModal"   data-backdrop="static"  data-keyboard="false" >
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title">Create new product</h4>
            </div>
            <div class="modal-body">
                <form role="form" action="{{ route('admin.product.addProduct')}}" method="post" id="frmProduct">
                    {{ csrf_field() }}
                    <input type="hidden" name="_token" value="{{ csrf_token() }}">
                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="">Product Name</label>
                                <input type="text" class="form-control" id="" name="product_name">
                            </div>
                            <div class="form-group">
                                <label for="">Product Type</label>
                                <input type="text" class="form-control" id="" name="product_type_id">
                            </div>
                            <div class="form-group">
                                <label for="">Price</label>
                                <input type="text" class="form-control" id="" name="price">
                            </div>
                            <div class="form-group">
                                <label for="">Status</label>
                                <select class="form-control input-sm m-bot15" id="" name="status">
                                    <option value="0">Inactive</option>
                                    <option value="1">Active</option>
                                </select>
                            </div>
                            <div class="form-group">
                                <label for="img">Image</label>

                                <div class="input-group">
                                    <span class="input-group-btn">
                                        <span class="btn btn-default btn-file">
                                            Choose <input type="file" id="imgInp">
                                        </span>
                                    </span>
                                    <input type="text" class="form-control" name="product_image" readonly>
                                </div>
                                <img id='img-upload' class="image_responsive" />
                            </div>
                        </div>

                        <div class="col-md-6">
                            <div class="form-group">
                                <label for="">Description</label>
                                <textarea class="form-control" id="" name="description"></textarea>
                            </div>
                            <div class="form-group">
                                <label for="">Note</label>
                                <textarea class="form-control" id="" name="addition_information"></textarea>
                            </div>

                        </div>
                    </div>
            </div>
            <div class="modal-footer">
                <input type="submit" value="Save" class="btn btn-success">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
            </form>
        </div>

    </div>
</div>