简体   繁体   English

触发选择不适用于触发事件

[英]Trigger select doesn't work with trigger event

To trigger input I can use focus(), but it doesn't work for select element. 要触发输入,我可以使用focus(),但不适用于select元素。 I tried trigger('change'), nothing happened. 我尝试了触发器('change'),没有任何反应。 Not sure what is wrong. 不知道出什么问题了。

DEMO https://jsfiddle.net/r8ca2jgr/ 演示https://jsfiddle.net/r8ca2jgr/

<select name="" id="">
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
</select>

<button>trigger</button>


$(function() {
  $('button').click(function() {
    $('select').trigger('change');
  })
});

Add a type for the button element: 为按钮元素添加类型:

<button type="button">trigger</button>

By default the type of the button is "submit" so your form will be submitted and you notice nothing for trigger change. 默认情况下,按钮的类型为“提交”,因此将提交您的表单,并且您不会发现触发器更改的任何内容。

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

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