Click (mousedown) event of a select element
I have an empty select element, I want to execute some code when that element is clicked on, but I do not want the dr开发者_开发知识库opdown list to appear, here is what I have found:
- When I disabled the select element I cannot catch the mousedown event.
- When I enable the select element I can capture the mousedown event but the dropdown list appears.
For arguements sake what I want is to be able to click the select element, JavaScript to throw an alert box but I want to prevent the dropdown list from displaying.
Thanks!
capture onmousedown
event of select element and cancel the event by setting event.returnValue
to false.
<script type="text/javascript">
function onSelectMouseDown() {
event.returnValue = false;
alert("mouse down caught and cancelled");
}
</script>
<select id="select" onmousedown="onSelectMouseDown();"></select>
Why not to disable a select element and wrap the select element in a div (or something else) of the same height and width as the select element and use mousedown event with this div?
<script>
function makeDisable(){
var x=document.getElementById("mySelect")
x.disabled=true
}
function makeEnable(){
var x=document.getElementById("mySelect")
x.disabled=false
}
function f() {
makeDisable();
alert("something");
t=setTimeout("makeEnable();",1);
}
</script>
<select id="mySelect" onMouseDown="f();">
<option value="opt1">Option 1</option>
<option value="opt2">Option 2</option>
</select>
精彩评论