引导程序模式:在输入字段中动态加载值 [英] Bootstrap modal: dynamically load value in input field

查看:67
本文介绍了引导程序模式:在输入字段中动态加载值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试动态加载的Laravel应用中有一个Bootstrap模态.想法是用来自数据库的数据填充模式中的输入字段,以便我可以更新值.我快到了....

I have a Bootstrap modal in a Laravel app that I'm trying to dynamically load. The idea is to fill the input fields in the modal with data coming from my database so I can update the values. I'm almost there....

我的查看文件中包含以下内容:

I have the following in my view file:

   <a href="#myModal" data-toggle="modal" data-target="#edit-auction-modal" data-id="1" data-title="title 1" type="button" class="btn btn-sm btn-primary btn-warning">Open modal</a>

"data-id"和"data-title"被传递给如下所示的JS函数:

The 'data-id' and 'data-title' are passed to a JS function that looks as follows:

$(function() {
    $('#edit-auction-modal').on("show.bs.modal", function (e) {
         $("#auctionLabel").html('Edit auction with id '+ $(e.relatedTarget).data('id'));
         $("#auctionTitle").html($(e.relatedTarget).data('title'));
    });
});

此JS函数成功捕获了ID和标题.在下面的模式视图中,我可以在面板标题中打印出ID,因为我要用JS告诉将数据放在id ="auctionLabel"中.

This JS function captures the id and title successfully. In the below modal view, I can print out the id in the panel-title because I'm telling in JS to put the data in the id="auctionLabel".

<div class="row">
   <div id="edit-auction-modal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="panel panel-info">
                  <div class="panel-heading">
                    <div class="panel-title" id="auctionLabel"></div>
                  </div>
                </div>
                <div class="modal-body edit-content" style="padding-top:10px">
                     <form  method="POST" action="{!! route('admin_auctions_update') !!}">
                      {{ csrf_field() }}
                      <div class="form-group">
                        <label for="auction_name" class="control-label">Auction name</label>
                        <input name="auction_name" id="name" class="form-control" placeholder="Auction name" value="{{$auction->auction_name}}">
                      </div>

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

我的问题是我需要如何更改Javascript函数,以便用标题的值预填充输入字段.换句话说,我希望输入框的值包含我在JS文件中捕获的标题".

My question is how I need to change the Javascript function in order to pre-fill the input fields with the value of the title. In other words, I want the value of the input box to contain the 'title' that I captured in the JS file.

推荐答案

尝试一下:

$('#edit-auction-modal').on("show.bs.modal", function (e) {
     $("#auctionLabel").html('Edit auction with id '+ $(e.relatedTarget).data('id'));
     $("#auctionTitle").html($(e.relatedTarget).data('title'));
    $('#edit-auction-modal ').find('input#input-id').val($(e.relatedTarget).data('title'))
});

这篇关于引导程序模式:在输入字段中动态加载值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆