1

我已经成功设置了一个从我的“multiselect_json.php”文件中动态填充的多选框。(这里的多选插件:https ://github.com/nicholasryan/jquery_jeditable/commit/1055cf6d51100d241b1e089c8cabcbad26207320 )

我的问题是我只能返回 1 个标记为“已选择”的值对

例子:

我的下拉选项如下: 1-product1 2-product2 3-product3

数组('选择')= 1;有效并正确突出显示此选项

但是当我指定 2 个选定选项时不起作用

数组('选择')= 1;

数组('选择')= 2;

在这种情况下,使用最后一个“选定”值 (2),而忽略前一个“选定”值 (1)。

这意味着如果在特定的多选列表中有许多预选项目,那么用户很难记住之前选择的内容,并且必须返回并重新选择之前手动选择的内容。

有谁知道如何使用 jeditable 插件为我的多选框传递多个“选定”值对?

jquery.jeditable.js

(函数($){

$.fn.editable = function(target, options) {

    if ('disable' == target) {
        $(this).data('disabled.editable', true);
        return;
    }
    if ('enable' == target) {
        $(this).data('disabled.editable', false);
        return;
    }
    if ('destroy' == target) {
        $(this)
            .unbind($(this).data('event.editable'))
            .removeData('disabled.editable')
            .removeData('event.editable');
        return;
    }

    var settings = $.extend({}, $.fn.editable.defaults, {target:target}, options);

    /* setup some functions */
    var plugin   = $.editable.types[settings.type].plugin || function() { };
    var submit   = $.editable.types[settings.type].submit || function() { };
    var buttons  = $.editable.types[settings.type].buttons 
                || $.editable.types['defaults'].buttons;
    var content  = $.editable.types[settings.type].content 
                || $.editable.types['defaults'].content;
    var element  = $.editable.types[settings.type].element 
                || $.editable.types['defaults'].element;
    var reset    = $.editable.types[settings.type].reset 
                || $.editable.types['defaults'].reset;
    var callback = settings.callback || function() { };
    var onedit   = settings.onedit   || function() { }; 
    var onsubmit = settings.onsubmit || function() { };
    var onreset  = settings.onreset  || function() { };
    var onerror  = settings.onerror  || reset;

    /* Show tooltip. */
    if (settings.tooltip) {
        $(this).attr('title', settings.tooltip);
    }

    settings.autowidth  = 'auto' == settings.width;
    settings.autoheight = 'auto' == settings.height;

    return this.each(function() {

        /* Save this to self because this changes when scope changes. */
        var self = this;  

        /* Inlined block elements lose their width and height after first edit. */
        /* Save them for later use as workaround. */
        var savedwidth  = $(self).width();
        var savedheight = $(self).height();

        /* Save so it can be later used by $.editable('destroy') */
        $(this).data('event.editable', settings.event);

        /* If element is empty add something clickable (if requested) */
        if (!$.trim($(this).html())) {
            $(this).html(settings.placeholder);
        }

        $(this).bind(settings.event, function(e) {

            /* Abort if element is disabled. */
            if (true === $(this).data('disabled.editable')) {
                return;
            }

            /* Prevent throwing an exeption if edit field is clicked again. */
            if (self.editing) {
                return;
            }

            /* Abort if onedit hook returns false. */
            if (false === onedit.apply(this, [settings, self])) {
               return;
            }

            /* Prevent default action and bubbling. */
            e.preventDefault();
            e.stopPropagation();

            /* Remove tooltip. */
            if (settings.tooltip) {
                $(self).removeAttr('title');
            }

            /* Figure out how wide and tall we are, saved width and height. */
            /* Workaround for http://dev.jquery.com/ticket/2190 */
            if (0 == $(self).width()) {
                settings.width  = savedwidth;
                settings.height = savedheight;
            } else {
                if (settings.width != 'none') {
                    settings.width = 
                        settings.autowidth ? $(self).width()  : settings.width;
                }
                if (settings.height != 'none') {
                    settings.height = 
                        settings.autoheight ? $(self).height() : settings.height;
                }
            }

            /* Remove placeholder text, replace is here because of IE. */
            if ($(this).html().toLowerCase().replace(/(;|"|\/)/g, '') == 
                settings.placeholder.toLowerCase().replace(/(;|"|\/)/g, '')) {
                    $(this).html('');
            }

            self.editing    = true;
            self.revert     = $(self).html();
            $(self).html('');

            /* Create the form object. */
            var form = $('<form />');

            /* Apply css or style or both. */
            if (settings.cssclass) {
                if ('inherit' == settings.cssclass) {
                    form.attr('class', $(self).attr('class'));
                } else {
                    form.attr('class', settings.cssclass);
                }
            }

            if (settings.style) {
                if ('inherit' == settings.style) {
                    form.attr('style', $(self).attr('style'));
                    /* IE needs the second line or display wont be inherited. */
                    form.css('display', $(self).css('display'));                
                } else {
                    form.attr('style', settings.style);
                }
            }

            /* Add main input element to form and store it in input. */
            var input = element.apply(form, [settings, self]);

            /* Set input content via POST, GET, given data or existing value. */
            var input_content;

            if (settings.loadurl) {
                var t = setTimeout(function() {
                    input.disabled = true;
                    content.apply(form, [settings.loadtext, settings, self]);
                }, 100);

                var loaddata = {};
                loaddata[settings.id] = self.id;
                if ($.isFunction(settings.loaddata)) {
                    $.extend(loaddata, settings.loaddata.apply(self, [self.revert, settings]));
                } else {
                    $.extend(loaddata, settings.loaddata);
                }
                $.ajax({
                   type : settings.loadtype,
                   url  : settings.loadurl,
                   data : loaddata,
                   async : false,
                   success: function(result) {
                      window.clearTimeout(t);
                      input_content = result;
                      input.disabled = false;
                   }
                });
            } else if (settings.data) {
                input_content = settings.data;
                if ($.isFunction(settings.data)) {
                    input_content = settings.data.apply(self, [self.revert, settings]);
                }
            } else {
                input_content = self.revert; 
            }
            content.apply(form, [input_content, settings, self]);

            input.attr('name', settings.name);

            /* Add buttons to the form. */
            buttons.apply(form, [settings, self]);

            /* Add created form to self. */
            $(self).append(form);

            /* Attach 3rd party plugin if requested. */
            plugin.apply(form, [settings, self]);

            /* Focus to first visible form element. */
            $(':input:visible:enabled:first', form).focus();

            /* Highlight input contents when requested. */
            if (settings.select) {
                input.select();
            }

            /* discard changes if pressing esc */
            input.keydown(function(e) {
                if (e.keyCode == 27) {
                    e.preventDefault();
                    reset.apply(form, [settings, self]);
                }
            });

            /* Discard, submit or nothing with changes when clicking outside. */
            /* Do nothing is usable when navigating with tab. */
            var t;
            if ('cancel' == settings.onblur) {
                input.blur(function(e) {
                    /* Prevent canceling if submit was clicked. */
                    t = setTimeout(function() {
                        reset.apply(form, [settings, self]);
                    }, 500);
                });
            } else if ('submit' == settings.onblur) {
                input.blur(function(e) {
                    /* Prevent double submit if submit was clicked. */
                    t = setTimeout(function() {
                        form.submit();
                    }, 200);
                });
            } else if ($.isFunction(settings.onblur)) {
                input.blur(function(e) {
                    settings.onblur.apply(self, [input.val(), settings]);
                });
            } else {
                input.blur(function(e) {
                  /* TODO: maybe something here */
                });
            }

            form.submit(function(e) {

                if (t) { 
                    clearTimeout(t);
                }

                /* Do no submit. */
                e.preventDefault(); 

                /* Call before submit hook. */
                /* If it returns false abort submitting. */                    
                if (false !== onsubmit.apply(form, [settings, self])) { 
                    /* Custom inputs call before submit hook. */
                    /* If it returns false abort submitting. */
                    if (false !== submit.apply(form, [settings, self])) { 

                      /* Check if given target is function */
                      if ($.isFunction(settings.target)) {
                          var str = settings.target.apply(self, [input.val(), settings]);
                          $(self).html(str);
                          self.editing = false;
                          callback.apply(self, [self.innerHTML, settings]);
                          /* TODO: this is not dry */                              
                          if (!$.trim($(self).html())) {
                              $(self).html(settings.placeholder);
                          }
                      } else {
                          /* Add edited content and id of edited element to POST. */
                          var submitdata = {};
                          submitdata[settings.name] = input.val();
                          submitdata[settings.id] = self.id;
                          /* Add extra data to be POST:ed. */
                          if ($.isFunction(settings.submitdata)) {
                              $.extend(submitdata, settings.submitdata.apply(self, [self.revert, settings]));
                          } else {
                              $.extend(submitdata, settings.submitdata);
                          }

                          /* Quick and dirty PUT support. */
                          if ('PUT' == settings.method) {
                              submitdata['_method'] = 'put';
                          }

                          /* Show the saving indicator. */
                          $(self).html(settings.indicator);

                          /* Defaults for ajaxoptions. */
                          var ajaxoptions = {
                              type    : 'POST',
                              data    : submitdata,
                              dataType: 'html',
                              url     : settings.target,
                              success : function(result, status) {
                                  if (ajaxoptions.dataType == 'html') {
                                    $(self).html(result);
                                  }
                                  self.editing = false;
                                  callback.apply(self, [result, settings]);
                                  if (!$.trim($(self).html())) {
                                      $(self).html(settings.placeholder);
                                  }
                              },
                              error   : function(xhr, status, error) {
                                  onerror.apply(form, [settings, self, xhr]);
                              }
                          };

                          /* Override with what is given in settings.ajaxoptions. */
                          $.extend(ajaxoptions, settings.ajaxoptions);   
                          $.ajax(ajaxoptions);          

                        }
                    }
                }

                /* Show tooltip again. */
                $(self).attr('title', settings.tooltip);

                return false;
            });
        });

        /* Privileged methods */
        this.reset = function(form) {
            /* Prevent calling reset twice when blurring. */
            if (this.editing) {
                /* Before reset hook, if it returns false abort reseting. */
                if (false !== onreset.apply(form, [settings, self])) { 
                    $(self).html(self.revert);
                    self.editing   = false;
                    if (!$.trim($(self).html())) {
                        $(self).html(settings.placeholder);
                    }
                    /* Show tooltip again. */
                    if (settings.tooltip) {
                        $(self).attr('title', settings.tooltip);                
                    }
                }                    
            }
        };            
    });

};


$.editable = {
    types: {
        defaults: {
            element : function(settings, original) {
                var input = $('<input type="hidden"></input>');                
                $(this).append(input);
                return(input);
            },
            content : function(string, settings, original) {
                $(':input:first', this).val(string);
            },
            reset : function(settings, original) {
              original.reset(this);
            },
            buttons : function(settings, original) {
                var form = this;
                if (settings.submit) {
                    /* If given html string use that. */
                    if (settings.submit.match(/>$/)) {
                        var submit = $(settings.submit).click(function() {
                            if (submit.attr("type") != "submit") {
                                form.submit();
                            }
                        });
                    /* Otherwise use button with given string as text. */
                    } else {
                        var submit = $('<button type="submit" />');
                        submit.html(settings.submit);                            
                    }
                    $(this).append(submit);
                }
                if (settings.cancel) {
                    /* If given html string use that. */
                    if (settings.cancel.match(/>$/)) {
                        var cancel = $(settings.cancel);
                    /* otherwise use button with given string as text */
                    } else {
                        var cancel = $('<button type="cancel" />');
                        cancel.html(settings.cancel);
                    }
                    $(this).append(cancel);

                    $(cancel).click(function(event) {
                        if ($.isFunction($.editable.types[settings.type].reset)) {
                            var reset = $.editable.types[settings.type].reset;                                                                
                        } else {
                            var reset = $.editable.types['defaults'].reset;                                
                        }
                        reset.apply(form, [settings, original]);
                        return false;
                    });
                }
            }
        },
        text: {
            element : function(settings, original) {
                var input = $('<input />');
                if (settings.width  != 'none') { input.attr('width', settings.width);  }
                if (settings.height != 'none') { input.attr('height', settings.height); }
                /* https://bugzilla.mozilla.org/show_bug.cgi?id=236791 */
                //input[0].setAttribute('autocomplete','off');
                input.attr('autocomplete','off');
                $(this).append(input);
                return(input);
            }
        },
        textarea: {
            element : function(settings, original) {
                var textarea = $('<textarea />');
                if (settings.rows) {
                    textarea.attr('rows', settings.rows);
                } else if (settings.height != "none") {
                    textarea.height(settings.height);
                }
                if (settings.cols) {
                    textarea.attr('cols', settings.cols);
                } else if (settings.width != "none") {
                    textarea.width(settings.width);
                }
                $(this).append(textarea);
                return(textarea);
            }
        },
        select: {
           element : function(settings, original) {
                var select = $('<select />');
                $(this).append(select);
                return(select);
            },
            content : function(data, settings, original) {
                /* If it is string assume it is json. */
                if (String == data.constructor) {      
                    eval ('var json = ' + data);
                } else {
                /* Otherwise assume it is a hash already. */
                    var json = data;
                }
                for (var key in json) {
                    if (!json.hasOwnProperty(key)) {
                        continue;
                    }
                    if ('selected' == key) {
                        continue;
                    } 
                    var option = $('<option />').val(key).append(json[key]);
                    $('select', this).append(option);    
                }                    
                /* Loop option again to set selected. IE needed this... */ 
                $('select', this).children().each(function() {
                    if ($(this).val() == json['selected'] || 
                        $(this).text() == $.trim(original.revert)) {
                            $(this).attr('selected', 'selected');
                    }
                });
                /* Submit on change if no submit button defined. */
                if (!settings.submit) {
                    var form = this;
                    $('select', this).change(function() {
                        form.submit();
                    });
                }
            }
        },
             selectmulti: {
           element : function(settings, original) {
                var select = $('<select />');
                $(this).append(select);
                               $(this).find('select').attr('multiple','multiple');
                return(select);
            },
            content : function(data, settings, original) {
                /* If it is string assume it is json. */
                if (String == data.constructor) {      
                    eval ('var json = ' + data);
                } else {
                /* Otherwise assume it is a hash already. */
                    var json = data;
                }
                for (var key in json) {
                    if (!json.hasOwnProperty(key)) {
                        continue;
                    }
                    if ('selected' == key) {
                        continue;
                    } 
                    var option = $('<option />').val(key).append(json[key]);
                    $('select', this).append(option);    
                }

                /* Loop option again to set selected. IE needed this... */ 
                $('select', this).children().each(function() {
                    if ($(this).val() == json['selected'] || 
                        $(this).text() == $.trim(original.revert)) {
                            $(this).attr('selected', 'selected');
                    }
                });
            }
        }
    },

    /* Add new input type */
    addInputType: function(name, input) {
        $.editable.types[name] = input;
    }
};

/* Publicly accessible defaults. */
$.fn.editable.defaults = {
    name       : 'value',
    id         : 'id',
    type       : 'text',
    width      : 'auto',
    height     : 'auto',
    event      : 'click.editable',
    onblur     : 'cancel',
    loadtype   : 'GET',
    loadtext   : 'Loading...',
    placeholder: 'Click to edit',
    loaddata   : {},
    submitdata : {},
    ajaxoptions: {}
};

})(jQuery);

multiselect_json.php

<?php


$projectid = $_POST['id'];
$inputvalue = $_POST['value'];

require_once('connect.php');

        $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
        if ($mysqli->connect_error) {
            die('Connect Error (' . $mysqli->connect_errno . ') '
                . $mysqli->connect_error);
                        }

 $array = explode ( '_' , $projectid ) ; // This will split $inputvalue by the character '_'
 $column = $array[0];
 $id = $array[1];

// get full list of products
$query = "SELECT DISTINCT * FROM products ORDER BY productname"; 
$result = mysqli_query($mysqli, $query);

// get selected productid's
$query2 = "SELECT DISTINCT productid FROM projectsproducts WHERE projectid='$id'";
$result2 = mysqli_query($mysqli, $query2);


while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) { 
    $id_1 = $row['id']; 
    $productname = $row['productname'];
    $array[$id_1] = $productname;


    };


    $array['selected'] = '2';
       $array['selected'] = '3';




print json_encode($array);  


 ?>
4

0 回答 0