none
ASP.NET 文本框值改变表单自动提交 RRS feed

  • 问题

  • 想实现的功能是当文本框值改变的时候如果内容不为空则提交表单,调用后台的textchanged事件里面的处理函数,如果为空,则不提交表单只在浏览器处理。但是无论怎样都提交了表单。。。即会进入服务器的处理事件,整了4天了还没解决,求大牛指教! 

    <script type="text/javascript">
                                    function WhenValueIsNull()
                                    {
                                        var temptext = document.getElementById("MainContent_txtCaseNumber");


                                        if (temptext.value == "")
                                        {
                                            alert("空");
                                           
                                        }
                                      
                                        if (temptext.value != "")
                                        {
                                            alert("非空");
                                            document.getElementById("Form1").submit();


                                        }
                                    }
                                </script>
                                <asp:TextBox ID="txtCaseNumber" runat="server"   OnTextChanged="txtCaseNumber_TextChanged"  onblur="WhenValueIsNull()" onsubmit="return false" AutoPostBack="True" Height="18px" Width="164px" ></asp:TextBox>

    2013年9月9日 7:41

全部回复

  • <asp:TextBox ID="txtCaseNumber" runat="server" onblur="WhenValueIsNull()" onsubmit="return false" AutoPostBack="True" Height="18px" Width="164px" ></asp:TextBox>

    删除 OnTextChanged="txtCaseNumber_TextChanged"


    e-mail:ist_te@hotmail.com


    2013年9月10日 7:31
  • 你好,

    onblur="WhenValueIsNull()"  和 OnTextChanged="txtCaseNumber_TextChanged" 不要一起使用。

    OnTextChanged 存在bug 必须是TextBox失去焦距或按回车才能触发。

    我建议你只在敲回车的时候去触发WhenValueIsNull():

    function WhenValueIsNull() { var temptext = document.getElementById("txtCaseNumber"); if (window.event.keyCode ==13) { if (temptext.value == "") { alert("空"); } if (temptext.value != "") { alert("非空"); document.getElementById("Form1").submit(); } }

    <asp:TextBox ID="txtCaseNumber" runat="server" onkeydown="WhenValueIsNull(this)"

    onsubmit="return false" AutoPostBack="True" Height="18px" Width="164px"

    OnTextChanged="txtCaseNumber_TextChanged" ></asp:TextBox>


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    2013年9月10日 9:08
    版主