javascript - onclick event does not get triggered after onchange - Stack Overflow

Steps to recreate this issue:Type something in the text boxRight after typing click the button.I foun

Steps to recreate this issue:

  1. Type something in the text box
  2. Right after typing click the button.
  3. I found that only onchange event was triggered.

Code:

<div>
    <input type="text" onchange="console.log('onchange');$('#message').css('display','none');" >
    <div id="message">
        Validating
    </div>
    <br>
    <input type="button" onclick="console.log('onclick');" value="click me">
</div>

DEMO

Steps to recreate this issue:

  1. Type something in the text box
  2. Right after typing click the button.
  3. I found that only onchange event was triggered.

Code:

<div>
    <input type="text" onchange="console.log('onchange');$('#message').css('display','none');" >
    <div id="message">
        Validating
    </div>
    <br>
    <input type="button" onclick="console.log('onclick');" value="click me">
</div>

DEMO

Share Improve this question edited Dec 17, 2012 at 6:44 Felix Kling 818k181 gold badges1.1k silver badges1.2k bronze badges asked Dec 17, 2012 at 6:39 TonyTony 3541 gold badge3 silver badges11 bronze badges 3
  • 1 If you move the button away from under the div you remove, it works jsfiddle/mplungjan/edR2E – mplungjan Commented Dec 17, 2012 at 6:48
  • When I try it in Chrome 24 Mac, I get both events logged. – Barmar Commented Dec 17, 2012 at 6:49
  • <input type="text" onchange="setTimeout(function(){ console.log('onchange');$('#message').css('display','none'); }, 250);"/> <input type="button" onclick="setTimeout(function(){ console.log('onclick'); }, 250);" value="click me"/> – DDK Commented Dec 17, 2012 at 6:55
Add a ment  | 

4 Answers 4

Reset to default 5

Its because you are changing the layout which changes the button position and the button mouseup event is not getting called. See Demo:

JS Bin

if you want to hide the div, by preserving the space it occupies, use:

$('#message').css('visibility','hidden');

New Demo with visibility hidden

For onclick it is necessary to occur mouse-down and mouse-up event due to position change of your button its not calling onclick

Here is Demo for plete event.

You're looking for keydown/press/up ...

The onkeydown event occurs when the user is pressing a key (on the keyboard)....

<div>
    <input type="text" onkeydown="console.log('onchange');$('#message').css('display','none');" >
    <div id="message">
        Validating
    </div>
    <br>
    <input type="button" onclick="console.log('onclick');" value="click me">
</div>​

here is the fiddle

http://jsfiddle/zQLFt/1/

The click event is bination of 2 events mousedown and mouseup. You can use mousedown instead.

<div>
    <input type="text" onchange="console.log('onchange');$('#message').css('display','none');" >
    <div id="message">
        Validating
    </div>
    <br>
    <input type="button" onmousedown="console.log('onclick');" value="click me">
</div>

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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信