我有一个输入表单,它允许我从多个选项中进行选择,并在用户更改选择时执行一些操作。例如,

<select onChange="javascript:doSomething();">
  <option>A</option>
  <option>B</option>
  <option>C</option>
</select>

现在,doSomething()只在选择改变时被触发。

我想在用户选择任何选项时触发doSomething(),可能是相同的选项。

我已经尝试使用“onClick”处理程序,但在用户开始选择过程之前被触发。

那么,是否有一种方法在用户每次选择时触发一个函数?

更新:

Darryl提出的答案似乎有效,但并不总是有效。有时,当用户单击下拉菜单时,甚至在用户完成选择过程之前,事件就会被触发!


当前回答

onclick方法并不是完全糟糕,但正如前面所说,当没有通过鼠标单击更改值时,它不会被触发。 但是可以在onchange事件中触发onclick事件。

<select onchange="{doSomething(...);if(this.options[this.selectedIndex].onclick != null){this.options[this.selectedIndex].onclick(this);}}">
    <option onclick="doSomethingElse(...);" value="A">A</option>
    <option onclick="doSomethingElse(..);" value="B">B</option>
    <option onclick="doSomethingElse(..);" value="Foo">C</option>
</select>

其他回答

因此,我的目标是能够多次选择相同的值,这基本上覆盖onchange()函数,并将其转化为有用的onclick()方法。

基于上面的建议,我想出了这个适合我的方法。

<select name="ab" id="hi" onchange="if (typeof(this.selectedIndex) != undefined) {alert($('#hi').val()); this.blur();}" onfocus="this.selectedIndex = -1;">
    <option value="-1">--</option>
    <option value="1">option 1</option>
    <option value="2">option 2</option>
    <option value="3">option 3</option>
</select>

http://jsfiddle.net/dR9tH/19/

我知道这个问题现在已经很老了,但是对于仍然遇到这个问题的人来说,我已经通过向我的选项标记添加onInput事件来实现了这一点,然后在被调用的函数中,检索该选项输入的值。

<选择id='dropdown' on输入='myFunction()'> <选项value = " 1 " > < /选项> <选项value = " 2 " > < /选项> 选择< - > < p >输出:< / p > <跨越id =“输出”> < /跨越> <类型=“短信/ javascript脚本”> 我的功能() var optionValue =文档。getElementById(“dropdown”)。 文件输出getElementById(“”)。内html =选择值; 的 剧本< - >

onclick方法并不是完全糟糕,但正如前面所说,当没有通过鼠标单击更改值时,它不会被触发。 但是可以在onchange事件中触发onclick事件。

<select onchange="{doSomething(...);if(this.options[this.selectedIndex].onclick != null){this.options[this.selectedIndex].onclick(this);}}">
    <option onclick="doSomethingElse(...);" value="A">A</option>
    <option onclick="doSomethingElse(..);" value="B">B</option>
    <option onclick="doSomethingElse(..);" value="Foo">C</option>
</select>

唯一正确的答案是不使用选择字段(如果您需要在重新选择相同的答案时执行某些操作)。

创建一个下拉菜单与传统的div,按钮,显示/隐藏菜单。链接:https://www.w3schools.com/howto/howto_js_dropdown.asp

如果能够将事件监听器添加到选项中,就可以避免这种情况。如果select元素有onSelect监听器。如果点击选择字段没有烦人地触发mousedown, mouseup,和同时点击mousedown。

我要扩展一下jitbit的答案。当你点击下拉菜单,然后不选择任何东西就关闭下拉菜单时,我觉得很奇怪。最后得出了如下结论:

var lastSelectedOption = null; DDChange = function(Dd) { //Blur after change so that clicking again without //losing focus re-triggers onfocus. Dd.blur(); //The rest is whatever you want in the change. var tcs = $("span.on_change_times"); tcs.html(+tcs.html() + 1); $("span.selected_index").html(Dd.prop("selectedIndex")); return false; }; DDFocus = function(Dd) { lastSelectedOption = Dd.prop("selectedIndex"); Dd.prop("selectedIndex", -1); $("span.selected_index").html(Dd.prop("selectedIndex")); return false; }; //On blur, set it back to the value before they clicked //away without selecting an option. // //This is what is typically weird for the user since they //might click on the dropdown to look at other options, //realize they didn't what to change anything, and //click off the dropdown. DDBlur = function(Dd) { if (Dd.prop("selectedIndex") === -1) Dd.prop("selectedIndex", lastSelectedOption); $("span.selected_index").html(Dd.prop("selectedIndex")); return false; }; <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id="Dd" onchange="DDChange($(this));" onfocus="DDFocus($(this));" onblur="DDBlur($(this));"> <option>1</option> <option>2</option> </select> <br/> <br/>Selected index: <span class="selected_index"></span> <br/>Times onchange triggered: <span class="on_change_times">0</span>

这对用户来说更有意义,并且允许JavaScript在每次选择任何选项(包括先前的选项)时运行。

这种方法的缺点是,它破坏了在下拉菜单上使用tab键并使用方向键选择值的功能。这对我来说是可以接受的,因为所有用户都会一直点击所有内容,直到永恒结束。