如何在通过单击超链接打开 bootstrap 模式后将值设置为出现在 bootstrap 模式上的文本框?

How to set value to a text-box present on bootstrap modal after it opened up by clicking on a hyperlink?

我在我的网站上使用 Bootstrap v3.3.5

我有一个超链接,单击它会打开一个 bootstrap 模式对话框。该超链接的HTML如下:

<a href="#myModal-add-event" data-toggle="modal">Add Event</a>

模态对话框HTML代码如下:

<div id="myModal-add-event" class="modal fade" role="dialog">
  <div role="document" class="modal-dialog add-event">
    <div class="modal-content">
      <div class="modal-body">
        <h4 class="modal-title event-title">Add an Event</h4>
        <form method="post" action="{$site_url}add_event.php" id="formAddEvent" >
          <!-- The gmail look alike loader should display here only upon successfull submission of a form. -->
          <div class="form-group" id="addEventErrorMsg" style="display:none; color:#FF0000;">
          </div>
          <div class="form-group">
            <input type="text" name="txt_event_title" id="txt_event_title"  autocomplete="off" class="form-control custom-height" placeholder="Event Title" style="height:30px;" />
           </div>
           <div class="form-group">
             <textarea type="text" name="txt_event_description" id="txt_event_description"  autocomplete="off" class="form-control custom-height" placeholder="Description (optional)" style="height:60px;" ></textarea>
           </div>
           <table border="0" cellspacing="10">
             <tr>
               <th><span class="event-title1" style="margin-bottom:5px;">Start Date:</span></th>
               <th><span class="event-title1" style="margin-bottom:5px;">End Date:</span></th>
             </tr>
             <tr>
               <td>
                 <div style="margin-right:15px;" class="form-inline form-group event-selection">
                 <div class="form-group has-feedback">
                   <div class='input-append date form_datetime' data-date="2013-02-21T15:25:00Z">
                     <input type='text' id='event_start_date' name="event_start_date" style="width:225px; display:inline; height:30px;" class="form-control" autocomplete="off" />
                     <span aria-hidden="true" class="glyphicon glyphicon-calendar form-control-feedback"></span>
                   </div>
                 </div>
               </div>
             </td>
             <td>
               <div class="form-inline form-group event-selection">
                 <div class="form-group has-feedback">
                   <div class='input-append date form_datetime' data-date="2013-02-21T15:25:00Z">
                     <input type='text' id='event_end_date' name="event_end_date" style="width:225px; display:inline;height:30px;" class="form-control" autocomplete="off" />
                     <span aria-hidden="true" class="glyphicon glyphicon-calendar form-control-feedback"></span>
                   </div>
                 </div>
               </div>
             </td>
           </tr>
         </table>   
         <div class="form-group has-feedback">
           <input type="text" name="txt_event_location" id="txt_event_location"  autocomplete="off" class="controls form-control custom-height" placeholder="Event Location" style="height:30px;" />
           <span class="glyphicon glyphicon-map-marker form-control-feedback" aria-hidden="true"></span>        
         </div>     
         <div style="clear:both;"> </div>
         <div id="map"></div>
         <div class="form-group">
           <input type="text" name="txt_event_room" id="txt_event_room"  autocomplete="off" class="form-control custom-height" placeholder="Room No." style="height:30px;" />
         </div>
         <div class="form-group">
           <div id="custom-templates">
             <input class="typeahead form-control custom-height" id="selected_groupname" name="selected_groupname" type="text" placeholder="Invite Group">
             <input type="hidden" name="selected_groupid" id="selected_groupid" value=""  />
           </div>
         </div>        
         <div class="modal-footer text-center">
           <button class="btn btn-primary" id="btn_add_event" type="button">Add Event</button>
           <button data-dismiss="modal" class="btn btn-default" type="button">Cancel</button>
         </div>
       </form>
     </div>
   </div>
 </div>
</div>

我想将值设置为 你好...你好吗? 进入 ID 为 'selected_groupname'

的文本框

为此,我尝试了以下代码,但它对我不起作用。

$(document).ready(function() {
  $(function() {
    $('#myModal-add-event').bind('show',function(){
      $("#selected_groupname").val('Hello...How are you?');
    });
  });
});

此外,我还检查了 Firebug 控制台。我也没有收到任何错误或警告。

请有人帮助我。

要为 bootstrap 模式捕获的事件是这样的:

$('#modalselector').bind('show.bs.modal'....

不确定是不是拼写错误,或者您是否在 selected_groupname 的 js 代码中遗漏了 #

$("#selected_groupname").val('Hello...How are you?');

试试这个:

$("a").click(function(){
   $("#selected_groupname").val(" Hello...How are you? ");
});

我认为您没有将函数绑定到模态的正确事件。

Bootstrap 模态框有一些 自定义 事件用于连接到模态功能:show.bs.modal, shown.bs.modal, hide.bs.modal, hidden.bs.modal, loaded.bs.modal.

您应该使用的事件是 show.bs.modal,它会在调用 show 实例方法时立即触发。

试试下面的代码:

$(document).ready(function() {
    $('#myModal-add-event').on('show.bs.modal',function(){
        $("#selected_groupname").val('Hello...How are you?');
    });
});

您可以在此处阅读有关 bootstrap 模态事件的更多信息:http://getbootstrap.com/javascript/#modals-events