问问题
70841 次
7 回答
50
此外,您需要单独调用页面生成的任何动态生成的选择元素
因此,最好的方法是重新绑定生成的选择并额外调用.material_select()
.
为了可重用性,您可以在元素更改时设置一个侦听器,然后在更新原始选择时触发该侦听器
// 1) setup listener for custom event to re-initialize on change
$('select').on('contentChanged', function() {
$(this).material_select();
});
// 2a) Whenever you do this --> add new option
$selectDropdown.append($("<option></option>"));
// 2b) Manually do this --> trigger custom event
$selectDropdown.trigger('contentChanged');
这样做的好处是只需要更新已更改的特定选择元素。
jsFiddle & Stack Snippets中的演示:
$(function() {
// initialize
$('.materialSelect').material_select();
// setup listener for custom event to re-initialize on change
$('.materialSelect').on('contentChanged', function() {
$(this).material_select();
});
// update function for demo purposes
$("#myButton").click(function() {
// add new value
var newValue = getNewDoggo();
var $newOpt = $("<option>").attr("value",newValue).text(newValue)
$("#myDropdown").append($newOpt);
// fire custom event anytime you've updated select
$("#myDropdown").trigger('contentChanged');
});
});
function getNewDoggo() {
var adjs = ['Floofy','Big','Cute','Cuddly','Lazy'];
var nouns = ['Doggo','Floofer','Pupper','Fluffer', 'Nugget'];
var newOptValue = adjs[Math.floor(Math.random() * adjs.length)] + " " +
nouns[Math.floor(Math.random() * nouns.length)];
return newOptValue;
}
body { padding: 25px}
<link href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.96.1/css/materialize.min.css" rel="stylesheet">
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.96.1/js/materialize.min.js"></script>
<button class="waves-effect waves-light btn" id="myButton">
Add New Option to Dropdown
</button>
<select id="myDropdown" class="materialSelect">
<option value="Happy Floof">Happy Floof</option>
<option value="Derpy Biscuit">Derpy Biscuit</option>
</select>
于 2015-04-07T14:50:57.170 回答
9
您可以在数据绑定成功后重新初始化 select 元素。像这样,
$('select').material_select();
与此类似:
var next_id = $(".mtr-select");
$.each(json, function(key, value) {
$(next_id).append($("<option></option>").attr("value", value.id).text(value.name));
});
$(next_id).material_select();
它通过在加载时创建 dom 对象将其选项值绑定到新ul>li
元素。
于 2015-04-07T13:53:01.623 回答
6
这是 MaterializeCss v0.96.1 的有效解决方案。在 0.97.0 版本中它不起作用:似乎有一个在 HTML 中附加插入符号的错误。
这里是 v0.97.0 的代码:
$(document).ready(function() {
// Initialize
$('select').material_select();
$("button").click(function() {
// Clear the content
$("select").empty().html(' ');
// And add a new value
var value = "New value";
$("select").append(
$("<option></option>").attr("value",value).text(value)
);
// Update the content clearing the caret
$("select").material_select('update');
$("select").closest('.input-field').children('span.caret').remove();
});
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.0/css/materialize.min.css" rel="stylesheet">
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.0/js/materialize.min.js"></script>
<button class="btn-large blue waves-effect waves-light">Change</button>
<div class="input-field">
<select>
<option>Option 1</option>
<option>Option 2</option>
<option>Option 3</option>
</select>
</div>
于 2015-07-11T17:57:51.557 回答
4
这适用于Materialize 1.0.0-rc.1:
情况:我有两个领域;首先是选择一个类别
<select name="category" id="category">
<option value="0">Choisissez une Catégorie</option>
<option value="1">Audios</option>
<option value="2">Vidéos</option>
<option value="3">Applications</option>
<option value="4">Jeux Vidéos</option>
</select>
选择类别后,第二个选择 id="subcategory" 根据父类别填充好的子猫:
<select name="subcategory" id="subcategory" disabled="disabled">
<option value="0">Choisissez une sous-catégorie</option>
</select>
var subCategoriesNames = ['Tout', ['Tout', 'Musiques', 'Concerts', 'Comédies'], ['Tout', 'Films', 'Séries TV', 'Emissions TV', 'Documentaires', 'Animations', 'Animations Séries', 'Concerts', 'Sports'], ['Tout', 'Livres', 'Magazines', 'Presses', 'Mangas', 'BD'], ['Tout', 'Formations', 'Android', 'Windows', 'Linux', 'Web', 'Emulateurs'], ['Tout', 'Android', 'Windows', 'Consoles', 'Linux']],
subCategoriesIds = ['1', ['2', '3', '4', '5'], ['6', '7', '8', '9', '10', '11', '12', '13', '14'], ['15', '16', '17', '18', '19', '20'], ['21', '22', '23', '24', '25', '26', '27'], ['28', '29', '30', '31', '32']],
idx = 0,
subsName;
$(document).ready(function(){
$('#category').on('change', function(){
idx = this.selectedIndex;
if(idx > 0){
$('select#subcategory').attr('disabled', false);
for(subsName in subCategoriesNames[idx]) $('select#subcategory').append('<option value="'+subCategoriesIds[idx][subsName]+'">'+subCategoriesNames[idx][subsName]+'</option>');
}else{
$('select#subcategory').attr('disabled', true);
}
var subcatSelectElem = document.querySelectorAll('#subcategory');
var subcatSelectInstance = M.FormSelect.init(subcatSelectElem, {});
})
});
于 2018-06-15T16:26:21.837 回答
3
嗨,当您使用物化框架时,使用 simple 生成相同的其他容器元素。因此使用 jquery select 标签不会做你需要的任务
我发现这对我来说很好
<select class="className"></select>
**$('.className').formSelect().append($('<option>'+data[i]+'</option>'))**
希望这也适合你
于 2019-02-17T07:54:18.150 回答
2
在 Materialize v1.0.0 中,您所要做的就是在从元素中$('.yourSelect').formSelect();
删除或添加option
元素后调用select
。
于 2019-10-17T05:45:15.823 回答
0
$('select').material_select(); // for initializing the material select box
$("select").closest('.input-field').children('.select-wrapper').children("span").html("");
于 2016-06-02T08:06:03.080 回答