简体   繁体   English

怎么听 <select>在物化css中更改事件

[英]How to listen on <select> change events in materialize css

A simple jquery listener on change doesn't seem to work when use a materialize css select dropdown. 使用materialize css select下拉列表时,一个简单的jquery监听器似乎不起作用。

 $("#somedropdown").change(function() {
         alert("Element Changed");
      }); 

1) How can I add a listener to detect when a materialize select element has changed? 1)如何添加侦听器以检测materialize select元素何时更改?

2) How do I get the select value in that case? 2)在这种情况下如何获得选择值?

Add an id to select 添加要选择的ID

 <select id="select1">
  <optgroup label="team 1">
    <option value="1">Option 1</option>
    <option value="2">Option 2</option>
  </optgroup>
  <optgroup label="team 2">
    <option value="3">Option 3</option>
    <option value="4">Option 4</option>
  </optgroup>
</select>

Bind event listener through jquery using the id 使用id通过jquery绑定事件监听器

$("#select1").on('change', function() {
    console.log($(this));
});

Hope this helps :-) 希望这可以帮助 :-)

JSFiddle Example Here JSFiddle示例在这里

I'm not sure how you are setting up the listener, but I tried out a basic case in codepen here: http://codepen.io/anon/pen/xVZZYy . 我不确定你是如何设置监听器的,但是我在codepen中尝试了一个基本的例子: http ://codepen.io/anon/pen/xVZZYy。

It definitely can detect changes as long as you place event listener onto the <select> element itself. 只要将事件侦听器放在<select>元素本身上,它肯定可以检测到更改。

不幸的是,materialze.css将<select>更改为基于<ul>和多个<li>的假选择,您可以尝试在生成的<li>上绑定事件,但这似乎很难:您无法绑定onchange如果你不使用browser-default类或者不搜索一下,在materialize.css <select>上的事件。

As you might know materialize adds it's own data-select-id so any Id you add to the select element will make it behave a bit unstable, what you can do is wrap your select element with a div and give it an ID then use jQuery to select that id and chain the select element like so.. 正如您可能知道的那样,materialize会添加它自己的data-select-id因此您添加到select元素的任何ID都会使其行为有点不稳定,您可以做的是用div包装您的select元素并给它一个ID然后使用jQuery选择那个id并链接select元素就像这样..

<div id="select1">
 <select >
  <optgroup label="team 1">
    <option value="1">Option 1</option>
    <option value="2">Option 2</option>
  </optgroup>
  <optgroup label="team 2">
    <option value="3">Option 3</option>
    <option value="4">Option 4</option>
  </optgroup>
</select>
</div>

and

$('#select1 select').on('change', function(){
    console.log("option selected!")
    // do your stuff here.
})

This worked for me. 这对我有用。

Add change event listener when you initialize material select 初始化材质选择时添加更改事件侦听器

$(document).ready(function(){          
          $('#somedropdown').material_select(someScope.someEvent.bind(someScope));
});

I am using materialize-css with angular7. 我正在使用带有angular7的materialize-css。 This is how I have handled this 这就是我处理这个问题的方法

<div class="input-field col s12">
    <select #qwcSelect (change)="optionChanged($event)">
        <option value="" selected disabled>{{placeHolder}}</option>
        // options
    </select>
    <label>{{label}}</label>
</div>

and in my component 在我的组件中

optionChanged(event) {
   console.log(`Selected Value ${event.target.value}`);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM