我有以下HTML:
<select class="select optional" data-minimumresultsforsearch="10"
data-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);}"
id="user_default_locale" name="user[default_locale]"></select>
我需要找到一个循环遍历所有元素的javascript函数,比如select。这就是我发现自己的一部分: - )
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]">
答案 0 :(得分: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);
});