javascript - Bootstrap modal: dynamically load value in input field - Stack Overflow

I have a Bootstrap modal in a Laravel app that I'm trying to dynamically load. The idea is to fill

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 ing 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>

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'));
    });
});

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>

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.

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 ing 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>

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'));
    });
});

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>

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.

Share Improve this question edited Jul 6, 2016 at 9:55 wiwa1978 asked Jul 6, 2016 at 9:48 wiwa1978wiwa1978 2,7275 gold badges38 silver badges76 bronze badges 3
  • <input name="auction_name" class="form-control" placeholder="Auction name" value="{{$auction->auction_name}}"> .Do you want to change this value? – Shashank Sood Commented Jul 6, 2016 at 9:51
  • Yes, I want to prefill that input field with the value I retrieve in Javascript. Note, there are more than 1 input fields in reality – wiwa1978 Commented Jul 6, 2016 at 9:56
  • $('#edit-auction-modal ').find('input#youid').val($(e.relatedTarget).data('title')) would solve the problem for you – Shashank Sood Commented Jul 6, 2016 at 9:58
Add a ment  | 

2 Answers 2

Reset to default 3

Try this :

$('#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'))
});

You can use .val( value ) to set/change the input value in jQuery.

http://api.jquery./val/#val2

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745315072a4622179.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信