我有以下 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>
我需要找到一个循环遍历所有元素(如 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]">