是否可以动态更改Bootstrap模式ID?

时间:2019-02-14 08:50:37

标签: jquery twitter-bootstrap twig

我的Twig循环正在生成Bootstrap卡。每张卡中都有一个按钮(#modal1)打开模式。我的问题是,当我在屏幕上有2张或更多张卡时,由于模式#1总是会给我第一张卡的ID,所以我无法获得这些卡的社区ID。

是否可以使用JS / Jquery更改id =“ modal1”和data-target =“#modal1”以使其与每个卡相对应?

谢谢。

<div class="row rowcards">
  {% for community in data %}
  <div class="col-md-3 removable">
    <div class="card" id="{{community.id}}"> 
      <div class="card-body">
        <a href="#" id="threedot" class="btn dropdown-toggle" datatoggle="dropdown" aria-haspopup="true" aria-expanded="false"><i class="fas fa-ellipsis-v"></i></a>
        <!-- menu -->
        <div class="dropdown-menu">
          <p class="text-center"><a href="#" class="modifylink">{{ 'Modifiy' | trans }}</a></p>
          <p class="text-center"><a href="#" data-id="{{community.id}}" class="text-danger deletelink"><i class="fas fa-trash-alt"></i>{{ 'Delete' | trans }}</a></p>
        </div>
        <p class="card-title">{{ community.info.name }}</p>
        <p class="card-text"><i class="fas fa-users"></i> 4 membres</p>
        <div class="text-center"> 
          <button class="btn btn-primary btn-card" data-toggle="modal" data-target="#modal1"><i class="fas fa-user-plus"></i> {{ 'Add member' | trans }}</button>
        </div>
      </div>
    </div>
    <div data-id="{{community.id}}" class="modifyForm">
      <div class="form-group">
        <small class="form-text text-muted text-center">{{ 'Change Community' | trans }}</small>
        <input data-id="{{community.id}}" maxlength="30" type="text" class="form-control" name="changename" required />
      </div>
      <button type="submit" data-id="{{community.id}}" class="btn btn-primary changecommunity">{{ 'Validate' | trans }}</button>
      <button type="submit" data-id="{{community.id}}" class="btn btn-danger toggleback">Annuler</button>
    </div>
  </div>
  {% endfor %}

模态:

<!-- Modal add member -->
{% for community in data %}
<div class="modal hide fade" id="modal1" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
      <div class="modal-dialog">
        <div class="modal-content" id="{{community.id}}">
          <div class="modal-body">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">&times;</span>
            </button>
            <h5 class="text-primary">Ajouter une personne à votre communauté</h5>
            <div class="form-group">
              <label for="create-givenname">{{ "Given name" | trans }}</label>
              <input name="givenName" type="text" class="form-control" id="create-givenname" aria-describedby="givennameHelp"
               placeholder="{{ "Given name" | trans }}"> 
            </div> 
            <div class="form-group">
              <label for="create-familyname">{{ "Family name" | trans }}</label>
              <input name="familyName" type="text" class="form-control" id="create-familyname" aria-describedby="familynameHelp"
               placeholder="{{ "Family name" | trans }}">
            </div>
            <div class="form-group">
              <label for="create-email">{{ "Email address" | trans }}</label>
              <input name="email" type="email" class="form-control" id="create-email" aria-describedby="emailHelp"
               placeholder="{{ "Email address" | trans }}">
            </div>
          </div>
          <div class="modal-footer">
            <button type="submit" data-id="{{community.id}}" class="btn btn-primary addmember" data-dismiss="modal">Valider</button>
          </div>
        </div>
      </div>
    </div>
   {% endfor %}

3 个答案:

答案 0 :(得分:0)

更新:在提问者发表评论后...创建/生成新卡片时,请确保

  1. 模态是通过

    生成的
    • modal1作为div的ID
    • modalToggle作为按钮的ID
    • 就像: <div id="modal1"> <button id='modalToggle' >'Add member' </button> </div>
  2. 我们仅在生成卡后立即调用函数afterCreatingNewCard,这将确保将默认ID(“ modal1”和“ moddleToggle”)替换为运行编号

    < / li>
  3. 我已针对您的具体情况更新了代码-如果有任何疑问,请告诉我

jquery attr可以帮助完成此任务...在下面进行检查

$(document).ready(function() {
  var currentRunningNumber = 1;
  
  yourLogicForCreatingACard(){
  //your normal logic will go here
  afterCreatingNewCard();
  }

  afterCreatingNewCard(){
    var newRunningNumber = currentRunningNumber + 1;
    var newModalName = "modal"+newRunningNumber;
    var newModalToggleName = "#modal"+newRunningNumber;
    
    $("#modal1").attr("id", newModalName);
    $("#modalToggle").attr("data-target", newModalToggleName);
    currentRunningNumber = newRunningNumber;
  }
 
  
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="modal hide fade" id="modal1" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <button id='modalToggle' class="btn btn-primary btn-card" data-toggle="modal" data-target="#modal1">'Add member' </button>
</div>

答案 1 :(得分:0)

您可以使用jquery更改ID。但是我不知道如何获取新的id值。你必须这样做。

$('.btn-card').click(function() {
    $('.modal').attr('id', newId);
    var Id = $('.modal').attr('id');
    $('#modal' + Id).modal('toggle');
});

答案 2 :(得分:0)

是的,可以动态更改ID。 我今天做了类似的事情。 我在代码中使用了一个动态谷值{{image_item.id}”,它为每个图像创建一个唯一的ID,并为这些图像创建一个唯一的模式弹出窗口。

浏览图像

'{% for image_item in images %}
        <div class="grid-item">

          <img src="{{image_item.image_file.url}}" height="500px" width="100%" data-toggle="modal" data-target="#img{{image_item.id}}"/>
        </div>

        {% endfor %}` 

模态代码。

`<!-- Modal starts here -->
        {% for image_item in images %}
        <div class="modal fade" id="img{{image_item.id}}" tabindex="-1" role="dialog" aria-labelledby="exampleModal3Label" aria-hidden="true">
          <div class="modal-dialog modal-lg" role="document">
            <div class="modal-content">
              <div class="modal-header">
                <h5 class="modal-title" id="exampleModal3Label">{{ image_item.image_name }}</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                  <span aria-hidden="true">&times;</span>
                </button>
              </div>
              <div class="modal-body">


                  <div class="">
                    <p> Bunch of codes here </p>
                  </div>


              </div>
              <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                <!-- <button type="button" class="btn btn-primary">Save changes</button> -->
              </div>
            </div>
          </div>
        </div>
        {% endfor %}`