开发者

HDI: Disable postback on html input box

开发者 https://www.devze.com 2023-01-25 00:33 出处:网络
I have an input box that I don\'t want postback to occur on someone striking the enter key I w开发者_开发技巧ant the javascript event to take place instead.

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

I w开发者_开发技巧ant 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;
        }
    }


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.com/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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号