2016-12-03 66 views
0

我真的需要一些幫助:我目前正在開發一個購物車。每當新產品附加到購物車時,它就成爲一個按鈕。該按鈕(產品)用於爲每個產品添加一些修飾符。按鈕定義可以看到下面:只要產品的每個按鈕的按下一個jQuery UI對話窗口Jquery UI可選:定義多個可選對象DYNAMICALLY

 var productAdded = $('<tr class="product" data-real_id = "'+ id +'" data-id_modal="'+ mod_id +'"><td class="product_name2"><button href="#0" class="button2" style="background-color:#00a65a;" data-comment="" data-modifiers="" data-span_mod = "" data-real_id = "'+ id +'" data-id_modall="'+ mod_id +'" id = "'+ comment_id +'">' + product_name + '</button></td><td class="quantity"><span class="select"><select id="cd-product-'+ id +'" name="quantity"><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option><option value="8">8</option><option value="9">9</option></select></span></td><td class="price">' + product_price + '</td><td><a href="#0" class="delete-item"><i class="fa fa-trash-o fa-2x" aria-hidden="true"></i></a></td></tr>'); 

打開whic包含一個jQuery UI可選擇的對象:

<div class="modal fade" id="modal">  
                <div class="modal-dialog">  
                 <div class="modal-content"> 
                 <div class="modal-header"></div> 
                 <div class="modal-body" style="margin-top:5px;"> 

                 <div style="width:100%; float:left;"> 
                  <label for="modifier">Τροποποιητές: </label> 
                  <span id="select-result" data-modifiers_span=""></span> 
                  <ol id="selectable" class="txt-modifiers"> 
                   <!-- INSERTED BY JQUERY DYNAMICALLY PER PRODUCT --> 
                  </ol> 
                 </div> 

                  <label for="comment">Σχόλιο: </label> 
                  <textarea rows="4" cols="50" name="comment" id="comment" value="" class="form-control txt-comment" style="vertical-align: top;"></textarea> 

                 </div> 
                 <div class="modal-footer" style="margin-top:10px;"> 
                 <button class="btn btn-success btn-save" data-id="" data-id_modall="">Αποθήκευση</button> 
                 <button class="btn btn-default" data-dismiss="modal">Κλείσιμο</button> 
                 </div> 
                </div> 
                </div> 
                </div> 

正如你可以看到下面

$(document).on('click touchstart','.button2',function(e){ 
         e.preventDefault(); 

         var id  = $(this).attr('id'); //Get element id 
         var real_id = $(this).attr('data-real_id'); 
         var comment = $(this).attr('data-comment'); //Get comment 
         var modifiers = $(this).attr('data-modifiers'); //Get modifiers 
         var teeee  = $(this).attr('data-id_modall'); 

         $('#modal .txt-comment').val(comment); 
         $('#modal .btn-save').attr('data-id',id); 
         $('#modal .btn-save').attr('data-id_modall',teeee); 

         //alert(modifiers); 
         if (modifiers.length == 0) 
         { 
         $("#selectable").html('<img src="images/ajax-loader.gif" />'); 
         var request = $.ajax({ 
          url:   'http://127.0.0.1:8080/Food%20Ball/backup/FoodBall%20Site%20form_dt_2/Food%20Ball%20Site/get_item_modifiers.php?item_id=' + real_id, 
          cache:  false, 
          dataType:  'json', 
          contentType: 'application/json; charset=utf-8', 
          type:   'get', 
          success: function(data) { 
          if(data.result != 'Not'){ 
           $("#selectable").html(data.options); 
           $('#modal .txt-modifiers').val(data.options); 
          } 
          else{ $("#selectable").html('Δεν υπάρχουν!'); 
            $('#modal .txt-modifiers').val('Δεν υπάρχουν!'); } 
          } 
         }); 
         } 
         else { $('#modal .txt-modifiers').val(modifiers); $("#selectable").html(modifiers);} 
         $('#modal').dialog('open'); 


        }); 

和可選擇對象:

產品的改性劑使用AJAZ + PHP從MySQL數據庫中提取
$(function() { 
         $('#selectable').on('touchstart mousedown', function(e) {e.metaKey = true;}) 
          .selectable({ 
            selected: function(event, ui) { 
            var result = $("#select-result").empty(); 
            $(".ui-selected", this).each(function() { 
             result.append($(this).attr('data-product_modifier') + ', '); 
            }); 
            },         
            unselected: function(event, ui){ 
             var result = $("#select-result"); 
            $(".ui-unselected", this).each(function() { 
             result.remove($(this).attr('data-product_modifier') + ', '); 
             }); 
            } 
           }); 
        }); 

終於爲每個對話框模態窗口中的「保存」按鈕:

$(document).on('click touchstart','.btn-save',function(e){ 
         e.preventDefault(); 
         var id =$(this).attr('data-id'); //Get data id 
         var comment =$('.txt-comment').val(); //get the comment 
         var modifiers =$('.txt-modifiers').val(); //get the modifier 

         //update the "order" note column modal 
         var note_modal = '#' + $(this).attr('data-id_modall'); //get the id row of the order modal 
         var note_modal2 = '#2' + $(this).attr('data-id_modall'); //get the id row of the order modal 

         $(note_modal).find('#note_modal').text(comment+'--'+modifiers); 
         $(note_modal2).find('#note_modal2').text(comment+'--'+modifiers); 

         $('#'+id).attr('data-comment',comment); 
         $('#'+id).attr('data-modifiers',modifiers); 


         //Save it in data base..s 
         $('#modal').dialog('close'); 
         $('.txt-comment').val('');//clear text area 
         $('.txt-modfiers').val('');//clear text area 
        }); 

        $(document).on('click','.btn-default',function(e){ 
         e.preventDefault(); 
         //Save it in data base..s 
         $('#modal').dialog('close'); 
         $('.txt-comment').val('');//clear text area 
         $('.txt-modfiers').val('');//clear text area       
        }); 

我的問題是,如果我一個以上的產品添加到我的購物車所選中調整出現的所有產品。我如何可以動態定義多個可選對象並保存每個產品的選定修飾符?

如果任何人都可以在此幫助,我將非常感激

謝謝

回答

0

好的解決了這個問題:

簡單地把下面的代碼Ajax調用成功返回內:

var request = $.ajax({ 
          url:   'http://127.0.0.1:8080/Food%20Ball/backup/FoodBall%20Site%20form_dt_2/Food%20Ball%20Site/get_item_modifiers.php?item_id=' + real_id, 
          cache:  false, 
          dataType:  'json', 
          contentType: 'application/json; charset=utf-8', 
          type:   'get', 
          success: function(data) { 
          if(data.result != 'Not'){ 
           $("#selectable").html(data.options); 
           $('#modal .txt-modifiers').val(data.options); 
            $("#selectable").selectable({ 
             stop: function() { 
              result_mod = $(".select_result").empty();            
              $(".ui-selected", this).each(function() { 
              result_mod.append(' +' + $(this).attr('data-product_modifier')); 
              //array_mod = array_mod + (' +' + $(this).attr('data-product_modifier')); 
              $('#modal .select_result').val(result_mod); 
             }); 
             } 
            }); 
          } 
          else{ $("#selectable").html('Δεν υπάρχουν!'); 
            $('#modal .txt-modifiers').val('Δεν υπάρχουν!'); 
            $(".select_result").html(''); 
            $('#modal .select_result').val('');          
           } 
          } 
         });