从postback onkeypress事件停止页面

时间:2012-09-21 13:57:50

标签: javascript asp.net firefox postback onkeypress

我有一个单页的asp.net 4.0表单,在div中有几个文本框,我以编程方式显示/隐藏。在我的表单上的一个特定文本框上,onkeypressevent像这样定义

<fieldset>
    <div class="WizardStepDiv">
        <label for="MobileNumber">
            Please enter your mobile number</label>
        <%--<asp:TextBox name="vmobilenumber" ID="vmobilenumber" runat="server" Style="width: 150px"></asp:TextBox>--%>
        <input type="text" name="vmobilenumber" id="vmobilenumber" runat="server" style="width: 150px"
            onkeypress="javascript: return searchKeyPress(event);" />
    </div>
    <div class="navigationArrows">
        <div style="margin-left: 35%; width: 165px;">
            <div class="previousbutton" style="float: left; padding: 3px 0 0 6px">
                <a href="#" onclick="NavigatetoPreviousScreen()">
                    <img src="image/left_arrow.png" width="18" height="25" alt="navigateprevious" style="background-image: url(image/arrow_box.png)" />
                </a>
            </div>
            <div class="nextbutton" style="float: right; padding: 3px 0 0 6px">
                <a href="#" id="searchMobileNumber" onclick="NavigatetoNextScreen()">
                    <img src="image/right_arrow.png" width="18" height="25" alt="navigatenext" style="background-image: url(image/arrow_box.png)" />
                </a>
            </div>
        </div>
    </div>
</fieldset>

searchKeyPress(事件)定义如下

function searchKeyPress(e) {
// look searchcheckoutKeyPress for window.event in case event isn't passed in
if (typeof e == 'undefined' && window.event) { e = window.event; }
if (e.keyCode == 13) {
    document.getElementById('searchMobileNumber').click();
    if (e.stopPropagation) {
        e.stopPropagation();
    } else {
        e.cancelBubble = true;
    }
  (event.preventDefault) ? event.preventDefault() : event.returnValue = false;
}

}

NavigatetoNextScreen()函数验证mobilenumber文本框是否为空,如果它通过验证,则会进行这样的ajax调用(下面部分删除)

    var mobilenumber = $.trim($("#vmobilenumber").val());
        if (mobilenumber == "" || !isNumber(mobilenumber)) {
            error += "<span>Please enter a valid mobile number to proceed to the next step</span></br>";

        }
        if (error == "") {
            $("#errormessage").hide();
            methodName = "GetVistorByMobileNumber";
            $.support.cors = true;
            $.ajax({
                type: 'GET',);

当存在验证错误时页面不回发,但是在ajax调用返回成功或错误(在两种情况下都经过处理之后)我不想要的网页回发。

此行为仅适用于Firefox,Chrome和IE 不回发。可能是什么问题?

提前感谢您的帮助

1 个答案:

答案 0 :(得分:0)

<a href="#" id="searchMobileNumber" onclick="NavigatetoNextScreen()">

在此函数YouNavigatetoNextScreen(evt)中传递事件对象并调用evt.preventDefault();

function YouNavigatetoNextScreen(){
   //... your code
   evt.preventDefault();
}