Bootstrap - 表单不在模态内提交

时间:2018-03-24 22:12:57

标签: javascript jquery twitter-bootstrap forms modal-dialog

我有一个在模态内部的表单,它不提交我的PHP操作。当我采用模态之外的形式时,它提交的很好,因为我想要它然而一旦在模态中提交变得没有响应,这是代码 我正在使用:

div class="modal fade bd-example-modal-lg" tabindex="-1" role="dialog" 
     aria-labelledby="myLargeModalLabel" aria-hidden="true" id="myLargeModalLabel">

  <div class="modal-dialog modal-lg">
<div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title">Add A New Appointment</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 id="submit_form">
                     <div class="row">
                         <div class="col">
                        <label>Name</label>  
                            <input type="text" name="fname" id="fname" class="form-control" /> 
                             </div>
                      <div class="col">                         
                        <label>Date</label>  
                            <input type="date" name="datepicker" id="datepicker" class="form-control" />
                         </div> 
                                                  </div>                                                  


                        <div class="form-group">
                            <label class="CustomLabel">Appointment Information</label>  
                     <textarea name="notes" id="notes" class="form-control"></textarea>  
                     </div> 
                     <input type="button" name="submit" id="submit" class="btn btn-info" value="Add Appointment" />  
                     <span id="error_message" class="text-danger"></span>  
                     <span id="success_message" class="text-success"></span>  
                </form> 

Javascript:

 $(document).ready(function(){  
  $('#submit').click(function(){ 
  var name = $('#fname').val();  
       var message = $('#notes').val();  
       if(name == '' || message == '')  
       {  
            $('#error_message').html("All Fields are required");  
       }  
       else  
   {  
            $('#error_message').html('');  
            $.ajax({  
                 url:"insert.php",  
                 method:"POST",  
                 data:{fname:name, notes:message},  
                 success:function(data){  
                      $("form").trigger("reset");  
                      $('#success_message').fadeIn().html(data);  
                      setTimeout(function(){  
                           $('#success_message').fadeOut("Slow");  
                      }, 2000);  
                    }  
               });  
            }  
        });  
    });  

1 个答案:

答案 0 :(得分:0)

请参阅此工作小提琴Working Fiddle of your Code

<form id="submit_form">
  <div class="row">
    <div class="col">
      <label>Name</label>
      <input type="text" name="fname" id="fname" class="form-control" />
    </div>
    <div class="col">
      <label>Date</label>
      <input type="date" name="datepicker" id="datepicker" class="form-control" />
    </div>
  </div>


  <div class="form-group">
    <label class="CustomLabel">Appointment Information</label>
    <textarea name="notes" id="notes" class="form-control"></textarea>
  </div>
  <input type="button" name="submit" id="submit" class="btn btn-info" value="Add Appointment" />
  <span id="error_message" class="text-danger"></span>
  <span id="success_message" class="text-success"></span>
</form>

JS代码

$(document).ready(function() {
  $('#submit').click(function() {
    var name = $('#fname').val();
    var message = $('#notes').val();
    if (name == '' || message == '') {
      $('#error_message').html("All Fields are required");
    } else {
      $('#error_message').html('');
      $.ajax({
        url: "insert.php",
        method: "POST",
        data: {
          fname: name,
          notes: message
        },
        success: function(data) {
          $("form").trigger("reset");
          $('#success_message').fadeIn().html(data);
          setTimeout(function() {
            $('#success_message').fadeOut("Slow");
          }, 2000);
        }
      });
    }
  });
});

我没有看到问题,除了没有加载jquery引用。您可以在浏览器的开发者控制台中看到这一点