0

我有以下 HTML:

<select class="select optional" data-minimumresultsforsearch="10" 
  data-query="function (query) {var data = {results: []}, i, j, s; for (i = 1; i &lt; 5; i++) {s = &quot;&quot;; for (j = 0; j &lt; i; j++) {s = s + query.term;} data.results.push({id: query.term + i, text: s});} query.callback(data);}" 
  id="user_default_locale" name="user[default_locale]"></select>

我需要找到一个循环遍历所有元素(如 select)的 javascript 函数。这就是我发现自己的部分:-)

document.getElementsByTagName('select')

但现在我需要这样称呼它们:

$("user_default_locale").select2({
            minimumresultsforsearch: 10,
            query: function (query) {
                var data = {results: []}, i, j, s;
                for (i = 1; i < 5; i++) {
                    s = "";
                    for (j = 0; j < i; j++) {s = s + query.term;}
                    data.results.push({id: query.term + i, text: s});
                }
                query.callback(data);
            }
        });

有人知道怎么做吗?

编辑:

好的,我现在有这个:

  $('select').each(function(idx, elem) {
  var $elem = $(elem);
  var VarMinimumResultsForSearch = $elem.data('minimumResultsForSearch');
  var VarPlaceholder = $elem.data('placeholder');

  $elem.select2({minimumResultsForSearch: VarMinimumResultsForSearch || undefined, placeholder: VarPlaceholder || undefined});
  });

有了这个,它确实执行了 select2 但不使用参数...

<select class="select optional" data-minimumresultsforsearch="1" data-placeholder="JOS" 
  id="user_default_locale" name="user[default_locale]">
4

1 回答 1

0

如果您要循环选择,您可能想尝试以下方法:

var funcs = {
    'func1': function() { .... },
    'func2': function() { .... }
}


$('select').each(function(idx, elem) {
    var $elem = $(elem);
    var foo = $elem.data('foo');
    var bar = $elem.data('bar');
    var funcKey = $elem.data('func-key');
    var callback = funcs[funcKey];

    $elem.select2(blah blah);
});
于 2013-01-01T12:45:07.337 回答