如何使用jquery打开选择输入 [英] How to open the select input using jquery

查看:126
本文介绍了如何使用jquery打开选择输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我可以看到select元素的选项,但我需要点击它。如果我想使用某个功能怎么办?当发生某些事情时,应选择此选择元素,表示列表已打开,我可以看到选项。我不希望用户点击选择元素,我想要其他东西来打开它。

So I can see the options of the select element but I need to click on it. What if I want to use a function? When something happens this select element should be selected, means the list is open and I can see the options. I don't want the user to click on the select element, I want something else to open it.

我尝试了什么

$("select").select();
$("select").click();
$("select").focus();

有一个select元素,通常如果你想打开它(下拉列表),你点击它。我想要的是打开它,如果我点击 DIV 或其他任何东西。我想让这个下拉列表打开,而无需用户点击select元素。

There is a select element, usually if you want it to open (the drop down list), you click on it. What I want is to open it if I click on a DIV or anything else. I want this drop down list to to open, without having the user clicking on the select element.

推荐答案

这应该有效:

var element = $("select")[0], worked = false;
if (document.createEvent) { // all browsers
    var e = document.createEvent("MouseEvents");
    e.initMouseEvent("mousedown", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
    worked = element.dispatchEvent(e);
} else if (element.fireEvent) { // ie
    worked = element.fireEvent("onmousedown");
}
if (!worked) { // unknown browser / error
    alert("It didn't worked in your browser.");
}

示例

这篇关于如何使用jquery打开选择输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆