刷新内容后引导模式内容问题

时间:2016-09-22 19:29:22

标签: javascript jquery ajax laravel

我有这个模态

   <!-- Modal -->
<div id="normale" class="modal fade" role="dialog">
  <div class="modal-dialog" style="background-color:white">

    <!-- Modal content-->
    <div class="modal-content" style="background-color:white">
      <div class="modal-header" style="background-color:white">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Aggiungi al carrello</h4>
      </div>
      <div class="modal-body" id="mod_carrello" style="background-color:white">
        <div class="hidden">
           <input type="text" id="id_tariffa" name="id_tariffa" value="id_tariffa" readonly class="form-control hidethis" style="display:none" >
        </div>
        <div class="form-group hidethis " style="display:none" >
          <label for="Id" class=" control-label col-md-4 text-left"> Id </label>
                    <div class="col-md-6">
                      <input type="text" id="id" name="id">
                     </div> 
                     <div class="col-md-2">

                     </div>
                    </div>          
                    <div class="form-group hidethis " style="display:none">
                    <label for="Cod Carrello" class=" control-label col-md-4 text-left"> Cod Carrello </label>
                    <div class="col-md-6">
                      <?php 
                       $persistent = DB::table('carrello')->where('entry_by', \Session::get('uid'))->first();
                       if (empty($persistent)) {
                        $chars = "abcdefghijkmnopqrstuvwxyz023456789"; 
                        srand((double)microtime()*1000000); 
                        $i = 0; 
                        $code = '' ; 
                        while ($i <= 20) { 
                        $num = rand() % 33; 
                        $tmp = substr($chars, $num, 1); 
                        $code = $code . $tmp; 
                        $i++;  } ?>
                      <input type="text" name="cod_carrello" value="{{ $code }}" id="cod_carrello">
                     <?php  } else {
                      $cod_carrello = $persistent->cod_carrello; ?>
                      <input type="text" name="cod_carrello" value="{{ $cod_carrello }}" id="cod_carrello">
                    <?php } ?>
                     </div> 
                     <div class="col-md-2">                
                     </div>
                    </div>              
                    <div class="form-group  " >
                    <label for="Quantita" class=" control-label col-md-4 text-left"> Quantita </label>
                    <div class="col-md-6">
                      <input type="text" class="form-control" id="quantita" name="quantita">
                     </div> 
                     <div class="col-md-2">
                     </div>
                    </div>            
                </fieldset>
              </div>
            <div class="modal-footer" style="background-color:white">
        <button type="submit" id="add_to_cart" class="btn btn-assertive">Aggiungi</button>
        <button type="button" class="btn btn-default" data-dismiss="modal">Chiudi</button>
      </div>
    </div>
  </div>
</div>

这是一个Ajax购物车。 这是Jquery脚本:

 $('#add_to_cart').click(function(){ 
  var button = $(event.relatedTarget) // Button that triggered the modal
  var id_tariffa = button.data('tariffa')
  // If necessary, you could initiate an AJAX request here (and then do the updating in a callback).
  // Update the modal's content. We'll use jQuery here, but you could use a data binding library or other methods instead.
  var modal = $(this)
    modal.find('.hidden input').val(id_tariffa);
  var cod_carrello = document.getElementById("cod_carrello").value;
  var quantita = document.getElementById("quantita").value;  
$.ajax({
  url: 'carrello-ajax',
  type: "post",
  data: {'id_tariffa':id_tariffa, 'cod_carrello':cod_carrello, 'quantita':quantita, '_token': $('input[name=_token]').val()},
  success: function(data){
    if ( data.OK == 1 ) {
    var mod_carrello_original = $("#mod_carrello").html();
    $('#mod_carrello').html('<h1 align="center" style="color:green; font-size: 21.5em;"><i class="fa fa-smile-o"></i></h1><br><br><h3 align="center">Servizio aggiunto correttamente al carrello</h3>');
    $("#normale").on("hidden.bs.modal", function(){
      $('#mod_carrello').html(mod_carrello_original);
    });
        } else {
    $('#mod_carrello').html('<h1 style="color:green"><i class="fa fa-error"></i><br>Si è verificato un errore</h2>');    
        }    
     }
  });
});      

});

我的问题是,在我输入2次后,添加到购物车中的某些产品没有任何问题,模式不再更新内容,也不会每次显示成功消息。 我希望有人可以帮助我。

1 个答案:

答案 0 :(得分:0)

使用具有不同内容的相同内容的新div解决