javascript 如何防止javascript回发

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4041896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 10:14:50  来源:igfitidea点击:

how to prevent postback by javascript

c#javascriptasp.net

提问by Nishant Kumar

actually this code is working well in firefox mozilla but it's not working in IE8

实际上,这段代码在 firefox mozilla 中运行良好,但在 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
    }

here i want to prevent postback when answerlength == 0 ;but when answer length ==0then it alert alert("u can't submit blank answer")message and postback to server io want to prevent it how i do ?

在这里,我想在什么answerlength == 0 ;时候阻止回发,但是什么时候answer length ==0它会alert("u can't submit blank answer")发出警报 消息并回发到服务器 io 想要阻止它,我该怎么办?

采纳答案by jgauffin

It's not working since you have a script error in your javascript.

它不起作用,因为您的 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>

回答by shashi

try this

试试这个

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