How to save and close a bootstrap modal?

后端 未结 6 1623
醉酒成梦
醉酒成梦 2021-02-13 06:17

This question have many answers out there but none of those answers solved my problem. I have a modal with some

提交评论

  • 2021-02-13 06:59

    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>
    
    0 讨论(0)
  • 2021-02-13 07:04

    First, you need to remove onclick="save()" from your button. You don't need that when you are using the on click function directly $('#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.

    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.

    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.

    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.

    0 讨论(0)
  • 2021-02-13 07:05

    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).

    You may have to remove the 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();
    
    });
    
    0 讨论(0)
  • 2021-02-13 07:05
    <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

    in the js function save() I didn't need to code anything but to save data into to my database

    // 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");
      }
    }
    
    0 讨论(0)
  • 2021-02-13 07:06

    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');
    
    0 讨论(0)
  • 提交回复
    热议问题