如何通过javascript防止回发

时间:2010-10-28 10:22:57

标签: c# javascript asp.net

实际上这段代码在firfox mozila中运行良好,但它在IE8中不起作用

<asp:Button ID="btnSubmit" runat="server" Text="Submit" CssClass="btnPrimary" OnClientClick="return doSubmit('this');"
        OnClick="btnSubmit_Click" />
      <script type="text/javascript">
           function doSubmit() 
           {
            var ansLength = oDOM.body.innerText.trim().length;
            if (ansLength == 0 && smielyPresent == -1) 
              {
              alert("you cannot submit blank answer");
              return false;
              }
           }
    </script>

     protected void btnSubmit_Click(object sender, EventArgs e)
    {
      // i am doing some stuff
    }

这里我希望在answerlength == 0 ;时阻止回发 但是当answer length ==0然后它警告alert("u can't submit blank answer")消息并回发到服务器时我想阻止它如何做?

2 个答案:

答案 0 :(得分:3)

由于您的javascript中存在脚本错误,因此无效。

<script type="text/javascript">
    function doSubmit() 
    {
        //I've removed the first equal sign
        var ansLength = oDOM.body.innerText.trim().length; 
        if (ansLength == 0 && smielyPresent == -1) //typo on smielyPresent ?
        {
            alert("u can't submit blank answer")
            return false;
        }
    }
</script>

答案 1 :(得分:2)

试试这个

OnClientClick="doSubmit(this); return false;"