[英]Trigger <select> click onClick of another element
I'm wanting to open a <select>
when clicking on it's parent .select
class. 我想在单击
<select>
的父.select
类时打开它。
https://jsfiddle.net/6o31gfwa/ - if you click the orange block you will see that the select opens, but then clicking on an option it doesn't close the select. https://jsfiddle.net/6o31gfwa/-如果单击橙色块,您将看到选择已打开,但是单击选项不会关闭选择。 How may I fix this?
我该如何解决?
<label class="select">
<select>
<option>Body Fat</option>
<option>Height</option>
<option>Jean Size</option>
<option>Chest</option>
<optio>Pulse</option>
</select>
</label>
$('body')
.on('click', '.select', function(event) {
event.preventDefault();
var i = $('.select').index($(this)),
e = document.createEvent('MouseEvents');
e.initMouseEvent('mousedown');
$('select')[i].dispatchEvent(e);
});
Yeah, right. 是的,对。 The previous answer will not work.
先前的答案将不起作用。 You can try this instead:
您可以尝试以下方法:
$('select.class-of-select').val(the_same_select_val).change();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.