I have an empty select element, I want to execute some code when that element is clicked on, but I do not want the dropdown 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!
I have an empty select element, I want to execute some code when that element is clicked on, but I do not want the dropdown 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!
Share Improve this question edited Dec 29, 2011 at 14:54 Rob W 349k87 gold badges807 silver badges682 bronze badges asked Jan 12, 2010 at 9:39 Ben EverardBen Everard 13.8k14 gold badges69 silver badges96 bronze badges3 Answers
Reset to default 3capture 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>
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745367778a4624661.html
评论列表(0条)