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

javascript - preventDefault in Safari - Stack Overflow

programmeradmin1浏览0评论

I have a textbox which has the code;

<input type="text" maxlength="5" size="2" value="1" id="paging_textbox">

Now there is no encloding form tag on this page..

When user types in this textbox and presses Enter, the table on this page is updated (via AJAX call)

In Safari, the behavior is slightly different in the sense that even though it updates the table correctly, it kind of reloads the page (think it submits the form, but not sure how as there is no form tag) and resets the textbox value back to "1"

How do I prevent this from happening in Safari ?

Please help me. Thank you.

I have a textbox which has the code;

<input type="text" maxlength="5" size="2" value="1" id="paging_textbox">

Now there is no encloding form tag on this page..

When user types in this textbox and presses Enter, the table on this page is updated (via AJAX call)

In Safari, the behavior is slightly different in the sense that even though it updates the table correctly, it kind of reloads the page (think it submits the form, but not sure how as there is no form tag) and resets the textbox value back to "1"

How do I prevent this from happening in Safari ?

Please help me. Thank you.

Share Improve this question asked May 31, 2011 at 10:07 copenndthagencopenndthagen 50.8k105 gold badges313 silver badges490 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

Just to be sure, terminate your callback function with a return false. That should do the trick.

 function(e) {
     e.preventDefault();
     // some awesome code
     return false;
 }

Oh, and another thing: in the future, to avoid such random behavior.. respect the standards, use a form :)

in jQuery

//Press Enter in INPUT moves cursor to next INPUT
$('#form').find(':input').keypress(function(e){
    if ( e.which == 13 )
    {
        $(this).next().focus();  //Use whatever selector necessary to focus the 'next' input
    }
});

JAVASCRIPT

<script language="JavaScript">
function disableEnterKey(e)
{
     var key;
     if(window.event)
          key = window.event.keyCode;     //IE
     else
          key = e.which;     //firefox
     if(key == 13)
          return false;
     else
          return true;
}
</script>

and write

<input type=”text” name=”mytext” onKeyPress=”return disableEnterKey(event)”>

use event.preventDefault() or return False should do the trick for you.

发布评论

评论列表(0)

  1. 暂无评论