Change value of materialize select box by jquery

2019-02-04 13:12发布

I want to change materialize select box value by jquery.

I am using $('#myselect').val('1'); on onchange event of other select box but it not works.

$("#select1").change(function() {
    $('#myselect').val('1');
});

9条回答
唯我独甜
2楼-- · 2019-02-04 14:03

As suggested by @logikal, you have got to re-Initialize

$("#myselect").material_select()
查看更多
混吃等死
3楼-- · 2019-02-04 14:06

For a new materialize 1.0.0 use .select() instead of .material_select()

Solution without re-initialization.

function msValue (selector, value) {
 selector.val(value).closest('.select-wrapper').find('li').removeClass("active").closest('.select-wrapper').find('.select-dropdown').val(value).find('span:contains(' + value + ')').parent().addClass('selected active');
}

Then just use

msValue($("#select_id"), "value_here")
查看更多
相关推荐>>
4楼-- · 2019-02-04 14:07

In 2018 (Materialize v1.0.0-rc.2), first you have to set your option programmatically:

$('#SELECT-ID').find('option[value="SELECT-VALUE"]').prop('selected', true);

And then re-initialise the select input with:

$("#SELECT-ID").formSelect();

Hope it helps!

查看更多
登录 后发表回答