简体   繁体   English

如何多次使用引导模式?

[英]How can I use a bootstrap-modal multiple times?

How can I use a dynamically generated Bootstrap-Modal multiple times with a default state? 如何在默认状态下多次使用动态生成的Bootstrap-Modal?

My problem is: When I click on the button to open the modal, you can see the default values 1 und 2. Now I enter '3' in the upper input field, close the modal with "Save changes" or "Close" and press the button again. 我的问题是:当我单击按钮以打开模式时,您可以看到默认值1和2。现在我在上方的输入字段中输入“ 3”,使用“保存更改”或“关闭”关闭模式。再次按下按钮。 Now, there should be 1 and 2 in the input fields but it's still 3 and 2. 现在,输入字段中应该有1和2,但仍然是3和2。

I tried to fix it in jquery (as you can see in the code), but it did not work: 我试图在jquery中修复它(如您在代码中可以看到的),但是它不起作用:

  $('#openModal1').on('click', function(){ let x = ` <form class="modal fade" id="modal-form" tabindex="-1" role="dialog" aria-labelledby="modal-dialogLabel" aria-hidden="true"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title" id="modal-dialogLabel">Modal title</h5> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">&times;</span> </button> </div> <div class="modal-body"> <!-- actual form markup --> <div class="form-group"> <label for="field1">Example label</label> <input name="field1" type="text" value="1" class="form-control" id="field1" placeholder="Example input"> </div> <div class="form-group"> <label for="field2">Another label</label> <input name="field2" type="text" value="2" class="form-control" id="field2" placeholder="Another input"> </div> <!-- /actual form markup --> </div> <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button> <button id="save" data-dismiss="modal" type="submit" class="btn btn-primary">Save changes</button> </div> </div> </div> </form> `; $("#modal-area").append(x); $('#modal-form').modal({}); }); $('#modal-form').on('hidden.bs.modal', function (event) { $("#modal-area").empty(); }); 
  <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/js/bootstrap.min.js"></script> <div id="modal-area"></div> <button id="openModal1" type="button" class="btn btn-primary" data-toggle="modal" data-target="#modal-form"> Open modal form </button> 

How can I solve this problem? 我怎么解决这个问题?

You need to use html() not append() see below 您需要使用html()而不是append()参见下文

  $('#openModal1').on('click', function(){ let x = ` <form class="modal fade" id="modal-form" tabindex="-1" role="dialog" aria-labelledby="modal-dialogLabel" aria-hidden="true"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title" id="modal-dialogLabel">Modal title</h5> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">&times;</span> </button> </div> <div class="modal-body"> <!-- actual form markup --> <div class="form-group"> <label for="field1">Example label</label> <input name="field1" type="text" value="1" class="form-control" id="field1" placeholder="Example input"> </div> <div class="form-group"> <label for="field2">Another label</label> <input name="field2" type="text" value="2" class="form-control" id="field2" placeholder="Another input"> </div> <!-- /actual form markup --> </div> <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button> <button id="save" data-dismiss="modal" type="submit" class="btn btn-primary">Save changes</button> </div> </div> </div> </form> `; $("#modal-area").html(x); $('#modal-form').modal({}); }); $('#modal-form').on('hidden.bs.modal', function (event) { $("#modal-area").empty(); }); 
  <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.3/js/bootstrap.min.js"></script> <div id="modal-area"></div> <button id="openModal1" type="button" class="btn btn-primary" data-toggle="modal" data-target="#modal-form"> Open modal form </button> 

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

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