可选的jQuery UI:动态定义多个可选对象

AA和平

在以下方面,我确实需要一些帮助:我目前正在开发购物车。每当有新产品添加到购物车时,它就会变成一个按钮。此按钮(产品)用于向每个产品添加一些修改器。按钮定义如下所示:

        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>');

每当按下产品的每个按钮时,都会打开一个JQUERY UI对话框窗口,其中包含一个JQUERY UI SELECTABLE对象:

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

如您在下面看到的,该产品的修饰符是使用AJAZ + PHP从mysql数据库中提取的:

$(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');


                    });

以及可选对象:

$(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                        
                    });

我的问题是,如果我在购物车中添加了不止一种产品,则所有产品都会出现所选的修饰符。如何动态定义多个可选对象并保存每个产品的所选修饰符?

如果有人可以提供帮助,我将非常感激

谢谢

AA和平

好的解决了:

只需将以下代码放入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('');                                     
                                }
                           }
                        });

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Swagger-指定可选对象属性或多个响应

来自分类Dev

jQuery UI可选-选择多个元素

来自分类Dev

使用jQuery使动态元素可选

来自分类Dev

Python-带可选对象的语句

来自分类Dev

TypeScript可选对象键行为异常

来自分类Dev

Swift函数-传递可选对象

来自分类Dev

使用可选对象解析JSON?

来自分类Dev

传递可选对象而不复制它

来自分类Dev

jQuery单击可选列表

来自分类Dev

Scala:访问可选对象中的可选值

来自分类Dev

jQuery UI可选和滚动条

来自分类Dev

带有JQuery UI的可选IMG

来自分类Dev

jQuery datepicker限制可选年份

来自分类Dev

从可选对象列表中返回对象列表

来自分类Dev

在if语句中评估可选对象的Bool属性

来自分类Dev

JSON模式:在可选对象类型上需要属性

来自分类Dev

具有嵌套可选对象的Mongoose模式

来自分类Dev

返回带有可选对象的可完成未来的列表

来自分类Dev

循环遍历 Swift 4 中的可选对象

来自分类Dev

为什么可选对象成员的类型为“从不”?

来自分类Dev

允许可选对象使用类扩展函数 Swift

来自分类Dev

jQuery不能绑定可选的单击事件

来自分类Dev

如何获取可选jquery元素的innerHTML?

来自分类Dev

可选和取消/全选按钮(jQuery)

来自分类Dev

jQuery可选单元格

来自分类Dev

在css类的jQuery表中可选

来自分类Dev

可选和取消/全选按钮(jQuery)

来自分类Dev

可选类型的jQuery选择器

来自分类Dev

jQuery可选mousedown取消选择元键