javascript - Click (mousedown) event of a select element - Stack Overflow

I have an empty select element, I want to execute some code when that element is clicked on, but I do n

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:

  1. When I disabled the select element I cannot catch the mousedown event.
  2. 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:

  1. When I disabled the select element I cannot catch the mousedown event.
  2. 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 badges
Add a ment  | 

3 Answers 3

Reset to default 3

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>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745367778a4624661.html

相关推荐

  • javascript - Click (mousedown) event of a select element - Stack Overflow

    I have an empty select element, I want to execute some code when that element is clicked on, but I do n

    16小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信