最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can my input element receive onclick events when it is set to disabled? - Stack Overflow

programmeradmin1浏览0评论

I need to have an onclick/hover event on an input tag which is disabled.
Is there another way but to wrap it in another tag and give that tag the events?

<input type="checkbox" onclick="cant_choose('event')" disabled="disabled" value="2" name="enroll_to[event][]">

I need to have an onclick/hover event on an input tag which is disabled.
Is there another way but to wrap it in another tag and give that tag the events?

<input type="checkbox" onclick="cant_choose('event')" disabled="disabled" value="2" name="enroll_to[event][]">
Share Improve this question edited May 21, 2012 at 9:51 THelper 15.6k6 gold badges67 silver badges105 bronze badges asked May 12, 2010 at 13:51 Itay Moav -MalimovkaItay Moav -Malimovka 53.6k67 gold badges196 silver badges290 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

You could simulate it being disabled with JavaScript and CSS.

That is, blur all focus it receives, and add a class with something like so.

input.disabled {
    background: #d4d0c8;
    color: #888;
    cursor: default;
}

Update

See it in parison with the normal and browser disabled input box on JSbin.

You could wrap the input field inside a container and bind the click event on it.

<div id='input-container'>
    <input id='myText' disabled />
</div>

<script>
    $('#input-container').click(function() {
        alert('test');
    });
</script>

http://jsfiddle/M2EYH/

If you have multiple disabled fields, and don't want to make an unique ID for each container, you can give the containers a class name instead of an ID.

发布评论

评论列表(0)

  1. 暂无评论