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

javascript - HDI: Disable postback on html input box - Stack Overflow

programmeradmin2浏览0评论

I have an input box that I don't want postback to occur on someone striking the enter key

I want the javascript event to take place instead.

<input 
type="text" 
id="addressInput" 
onkeydown="inputenter()" 
autopostback="false"/>

    function inputenter() {
        if (event.keyCode == 13) {
            seachLocations();
            return false;
        }
        else {
            return false;
        }
    }

I have an input box that I don't want postback to occur on someone striking the enter key

I want the javascript event to take place instead.

<input 
type="text" 
id="addressInput" 
onkeydown="inputenter()" 
autopostback="false"/>

    function inputenter() {
        if (event.keyCode == 13) {
            seachLocations();
            return false;
        }
        else {
            return false;
        }
    }
Share Improve this question edited Nov 15, 2010 at 20:45 Chris Hayes asked Nov 15, 2010 at 20:25 Chris HayesChris Hayes 4,0337 gold badges44 silver badges75 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

Just return false form JS function, add return false; at the end of function.

or <input type="text"
id="addressInput"
onkeydown ="return (event.keyCode!=13)" autopostback="false"/>

UPDATE: How about this..?

<asp:TextBox ID="TextBox1" runat="server" 
 onkeydown="return CallEnterKeyFunc(event.keyCode);"> 
</asp:TextBox>
    <script type="text/javascript">
    function CallEnterKeyFunc(key) {
        if (key == 13) { //for FF, i think you have to use evt.which
                //enter key press
                seachLocations();
                return false;

            }
            else
                return true;

    }

    function seachLocations() {
       //your code
        alert("hello");
    }
</script>

Special thanks to: http://www.beansoftware./ASP.NET-Tutorials/Accept-Enter-Key.aspx

<input type="text" id="addressInput" onkeydown="if (window.event.keyCode == 13) 
{
    event.returnValue=false; 
    event.cancel = true;
    searchLocations();
}" />
发布评论

评论列表(0)

  1. 暂无评论