简体   繁体   中英

How to prevent page reload after alert box on key down event?

I want to prevent page reload after clicking ok on alert box. click search button is ok. But now I meet problem in enter key down event. Someone help me please.

This is my html

<div id="email">
 <input type="text" id="txtSearchEmail" name="email" onkeydown="Javascript: if (event.keyCode==13) {JS_Search(); event.preventDefault();}" style="width: 161px; height: 16px; padding: 2px; border: 1px solid #a8a8a8; font-size: 13px; color: #434343" />
</div>
<div id="search">
<a class="gai-button" style="width: 96px; height: 25px;" onclick="JS_Search();"><img  src="images/findamessage_search.jpg" alt="" /></a>
</div>

This is my javascript

if() {
//some coding
}
else {
alert ("Not found!");
return false;
}

Remove "Javascript: " from your onkeydown handler, text in this attribute already Javascript. If this button in <form> tag - use "onsubmit" attribute/event in <form> tag.

Maybe this is what you want, maybe not, in this example i use jQuery:

$(function() {

    $("#search a.gai-button").click(function(ev) {

          if() {
               //some coding
          }
          else {
               alert ("Not found!");
               //This prevents the a tag from 
               ev.preventDefault();
               return false;
          }
    })

};

})

Add break; at the end of an event and modify your ifthenelse block to reload window when true .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM