简体   繁体   English

如何用同一个类打开多个模态

[英]How to open multiple modals with same class

When I click on 'send message' button multiple modals open, what I want is only one modal opens. 当我单击“发送消息”按钮时,会打开多个模式,我只想打开一个模式。

So my question is: How to open the first element of multiple bootstrap modals with the same class? 所以我的问题是:如何用同一个类打开多个引导程序模态的第一个元素?

My html code: 我的html代码:

<!-- button -->                                                    
<div class="btn-group btn-group-xs">
  <button type="button" class="btn btn-default tooltiped" data-toggle="tooltip" title="Send a message" style="min-width:65px;" onclick="message_modal(46)"><i class="fa fa-envelope"></i> Message</button>
</div>    

<!-- message Modal -->
<div class="modal fade message-modal-46"  tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="false">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header message-modal-header"> 
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
        <h4>Send message to ferair</strong></h4>
      </div>
      <div class="modal-body message-body">
        <div class="alert-notify-modal"></div>
        <form class="orb-form">
          <section>
            <label class="label"><strong>Votre message</strong></label>
            <label class="textarea">
            <textarea class="message-content-form" rows="3" placeholder="Write your message here" name"message-content-form"></textarea>
            </label>
           </section>
         </form>
       </div>

       <div class="data-thread-id hidden" data-thread-id="43"></div>
       <div class="data-receiver-id hidden" data-receiver-id="46"></div>

       <div class="modal-footer message-footer">
         <button type="button" class="btn btn-default modal-close" data-dismiss="modal">Close</button>
         <button type="button" class="btn btn-primary modal-message-submit-btn">send</button>
       </div>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->                          
<!--/ Message Modal -->

JQUERY: JQUERY:

var session_user_id = 1;

//Open message modal
function message_modal(type_id){

   //If not loggedin redirect to login page
   if(!session_user_id){

      window.location.href = base_url+"users/login";
      return;
   }

   //We refresh the div to show message modal 
   $(".message-modal-"+type_id).load(location.href+" .message-modal-"+type_id+">*", function () {

     $(".message-modal-"+type_id).modal('show');

   });
}

JSFIDDLE JSFIDDLE

There are a few typos in your code, which cause the issue you're running into. 您的代码中有一些错别字,这会导致您遇到问题。

First, this line: <h4>Send message to ferair</strong></h4> . 首先,这一行: <h4>Send message to ferair</strong></h4> You are closing a strong tag, but you never opened one. 您正在关闭一个强大的标签,但从未打开过。 This is true for both modals. 两种模态都是如此。

Secondly, this line: <textarea class="message-content-form" rows="3" placeholder="Write your message here" name"message-content-form"></textarea> The name you give does not have a = sign after it. 其次,此行: <textarea class="message-content-form" rows="3" placeholder="Write your message here" name"message-content-form"></textarea>您提供的名称没有=在它后面签名。 Again this is an issue on both modals. 同样,这是两个模态的问题。

Lastly, you use the same ID for both modals. 最后,您对两个模态使用相同的ID。 Because of this, both match and are activated. 因此,匹配和都被激活。 If you use unique IDs, your code works perfectly . 如果您使用唯一的ID,则您的代码可以完美运行

Edit: I mis-read the question, you wish to keep the classes unchanged. 编辑:我读错了问题,您希望保持类不变。 You can select the first result of a JQuery selection with the .first() function. 您可以使用.first()函数选择JQuery选择的第一个结果。 If you update your Javascript to this , it'll work: 如果您将Javascript更新为this ,那么它将起作用:

var session_user_id = 1;

//Open message modal
function message_modal(type_id){
  //If not loggedin redirect to login page
  if(!session_user_id){
    window.location.href = base_url+"users/login";
    return;
  }

  //We refresh the div to show message modal 
  $(".message-modal-"+type_id).first().load(location.href+" .message-modal-"+type_id+">*", function () {
    $(".message-modal-"+type_id).first().modal('show');
  });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM