简体   繁体   English

如何保存和关闭引导模式?

[英]How to save and close a bootstrap modal?

This question have many answers out there but none of those answers solved my problem.这个问题有很多答案,但这些答案都没有解决我的问题。 I have a modal with some <select> tags.我有一个带有一些<select>标签的模式。 I am trying to save the values from the select options .我正在尝试保存select options中的值。 after they click Save the modal should close but not submit the result yet because the user still need to review stuff after they click save on the modal.在他们单击“ Save ”后,模态应该关闭但尚未submit结果,因为用户在模态上单击“ save ”后仍需要查看内容。

HTML HTML

 <!-- Modal -->
 <div id="1" class="modal fade" role="dialog">
 <div class="modal-dialog">

  <!-- Modal content-->
  <div class="modal-content">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal">&times;       </button>
    <h4 class="modal-title">Modal Header</h4>
  </div>
  <div class="modal-body">
    //select tags go here
  </div>
  <div class="modal-footer">
    <button type="button" class="btn btn-default"    data-dismiss="modal">Close</button>
    <button id="save" onclick="save()" class="btn btn-width bkgrnd-cyan save-details" type="button" name="save-details">Save</button>
  </div>
</div>

JS JS

$('#save').click(function() {
$('#1').modal('hide');
 });

Since my code is really long, I just copied a modal from google but I added my Save button, so this is the real Save button that my modal is using.由于我的代码真的很长,我只是从谷歌复制了一个模态,但我添加了我的Save按钮,所以这是我的模态使用的真正的Save按钮。

Thanks谢谢

EDIT编辑

I changed the id number to letters so now the code doesn't include any id starting with number, thanks.我将 ID 号更改为字母,所以现在代码不包含任何以数字开头的 ID,谢谢。

PS still need help =/ PS 仍然需要帮助 =/

EDIT编辑

I also tried this one and it did not work我也试过这个,但没有用

$(document).ready(function(){

 $('#save').click(function(){
$('#modal_1').modal('hide');//modal_1 is the id 1
 }); 

}); 

HTML HTML

<div id="someModalId" class="modal fade" role="dialog">
 <div class="modal-dialog">

  <!-- Modal content-->
  <div class="modal-content">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal">&times;       </button>
    <h4 class="modal-title">Modal Header</h4>
  </div>
  <div class="modal-body">
    <select id="exampleSelect">
    </select>
  </div>
  <div class="modal-footer">
    <button type="button" class="btn btn-default"    data-dismiss="modal">Close</button>
    <button id="save" onclick="save()" class="btn btn-width bkgrnd-cyan save-details" type="button" name="save-details">Save</button>
  </div>
</div>

JS JS

$('#save').click(function() {
  $select_value = $("#exampleSelect").value();
  $('#someModalId').modal('hide');
});
  1. Give your select a name or id 选择一个名称或ID
  2. Set the value of select to a global variable. 将select的值设置为全局变量。 Make sure to declare the variable at the top if you are getting an error. 如果收到错误,请确保将变量声明在顶部。

I'm not sure the id=1 is fine, but you can use this code to simulate a click on the close button (or you can add an id or a class to it). 我不确定id=1是否合适,但您可以使用此代码模拟关闭按钮上的单击(或者您可以向其添加id或类)。

You may have to remove the onclick="save()" 您可能必须删除onclick="save()"

data = {};
$("#save").on("click", function(e){
  e.preventDefault(); // prevent de default action, which is to submit
  // save your value where you want
  data.select = $("#exampleSelect").value();
  // or call the save function here
  // save();
  $(this).prev().click();

});
<button id="save" class="btn btn-width bkgrnd-cyan save-details" type="button" name="save-details" onclick="save()" data-toggle="modal" data-target="#myModalList" data-dismiss="modal">Save</button>

this worked very well for me, and its a simple way to do it. 这对我来说非常有效,而且这是一个简单的方法。 after the onclick I used the data-toggle, data-target and data-dismiss 在onclick之后我使用了数据切换,数据目标和数据消除

in the js function save() I didn't need to code anything but to save data into to my database 在js函数中save()除了将数据保存到我的数据库之外,我不需要编写任何代码

// Insere notas na base de dados
function save() {
  console.log("func guardar");
  if ($("#message-text").val() == "") {
    alert("Texto vazio");
  } else {
    $(document).ready(function () {
      $.ajax({
        method: "POST",
        url: "../portal/portalphp/notas.php",
        data: {
          num: num,
          text: $("#message-text").val()
        }
      }).done(function (msg) {
        if (msg == 1) {
          //alert("Nota guardada com sucesso");
          readyNotas(num);
          console.log("func guardar done == 1");
        } else {
          alert("Erro: não foi possivel guardar");
        }
      });
      texto.value = "";
    });
    console.log("func guardar ready");
  }
}

First, you need to remove onclick="save()" from your button. 首先,您需要从按钮中删除onclick="save()" You don't need that when you are using the on click function directly $('#save').click(function()... 当您直接使用on click函数$('#save').click(function()...时,您不需要它$('#save').click(function()...

As was pointed out in the comments by @Eshwaren, you can't use a number to start an id , so you need fix that as well. 正如@Eshwaren在评论中指出的那样,你不能使用数字来启动id ,所以你也需要修复它。

To get the value from a select, you have to be able to identify it. 要从选择中获取值,您必须能够识别它。 A simple solution would be to give your select element an ID . 一个简单的解决方案是为您的select元素提供一个ID

For example: 例如:

<div class="modal-body">
    <select id="data_1">
    </select>
</div>

In your code, you can then assign the value of the select element to a variable. 在代码中,您可以将select元素的值赋给变量。

For example: 例如:

var data_1;
$('#save').click(function() {
  data_1  = $("#data_1").value();
  $('#modalid').modal('hide');
});

You can then use that variable elsewhere in your code. 然后,您可以在代码中的其他位置使用该变量。

There are many more possibilities for solving this, but the root of the issue is being able to identify the select elements in code and recording their respective values. 解决这个问题的可能性还有很多,但问题的根源是能够识别代码中的选择元素并记录它们各自的值。

thats my modal window 多数民众赞成我的模态窗口

$(function () {
    $("#dialog").dialog({
        modal: true,
        autoOpen: false,
        title: "Column Preferences",
        button: "save",
        width: 750,
        height: 620
}); 

try this to close or hide the window 尝试此操作来关闭或隐藏窗口

$('#dialog').dialog('close');

I finally got this working with a hint from Ricardo Almeida: 我终于得到了里卡多·阿尔梅达的暗示:

<%=form_with id: :friend_email_form, url: friend_emails_create_path do |f|%>

                      # form fields entered here

<div class="actions">
    <%= f.submit "Send Email", class: 'btn btn-primary', "onclick":"submit_form();", "data-dismiss":"modal"%>
</div>

 <script>
    function submit_form() {
      document.getElementById('friend_email_form').submit();
    }
</script>

just add 'data-dismiss="modal"' when click save单击保存时只需添加 'data-dismiss="modal"'

example:例子:

 <,DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width. initial-scale=1:0" /> <title>Document</title> <link rel="stylesheet" href="https.//cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/css/bootstrap.min;css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"> <body> <,-- Button trigger modal --> <button type="button" id="test2" class="btn btn-primary" data-toggle="modal" data-target="#test"> Launch demo modal </button> <,-- Modal --> <div class="modal fade" id="test" tabindex="-1" role="dialog" aria-labelledby="test" aria-hidden="true"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title" id="exampleModalLabel">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"> <form> <div class="form-row"> <div class="form-group col-md-6"> <label for="inputEmail4">Email</label> <input type="email" class="form-control" id="inputEmail4" placeholder="Email"> </div> <div class="form-group col-md-6"> <label for="inputPassword4">Password</label> <input type="password" class="form-control" id="inputPassword4" placeholder="Password"> </div> </div> <div class="form-group"> <label for="inputAddress">Address</label> <input type="text" class="form-control" id="inputAddress" placeholder="1234 Main St"> </div> <div class="form-group"> <label for="inputAddress2">Address 2</label> <input type="text" class="form-control" id="inputAddress2" placeholder="Apartment. studio. or floor"> </div> <div class="form-row"> <div class="form-group col-md-6"> <label for="inputCity">City</label> <input type="text" class="form-control" id="inputCity"> </div> <div class="form-group col-md-4"> <label for="inputState">State</label> <select id="inputState" class="form-control"> <option selected>Choose...</option> <option>:..</option> </select> </div> <div class="form-group col-md-2"> <label for="inputZip">Zip</label> <input type="text" class="form-control" id="inputZip"> </div> </div> <div class="form-group"> <div class="form-check"> <input class="form-check-input" type="checkbox" id="gridCheck"> <label class="form-check-label" for="gridCheck"> Check me out </label> </div> </div> </form> </div> <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button> <button type="submit" class="btn btn-primary" data-dismiss="modal">Save changes</button> </div> </div> </div> </div> <script src="https.//code.jquery.com/jquery-3.2.1:slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script> <script src="https.//cdn.jsdelivr.net/npm/popper.js@1.12:9/dist/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script> <script src="https.//cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script> </body> </html>

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

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