如何防止文本框失去焦点后提交按钮的提交

时间:2018-06-26 21:36:19

标签: javascript asp.net submit onchange

我有一个带有onchange事件的文本框。该函数触发时,将显示一个隐藏元素。但是,当由于单击提交按钮而触发onchange时,该元素会非常短暂地显示,因为submit函数会在之后立即触发。如果是导致文本框“失去焦点”的元素,有什么方法可以防止提交按钮不触发?

编辑:

jquery 1.3.2

<script type="text/javascript">
    function phoneChanged(currentElement, valueToCompareAgainst) {
        $('#divPhoneChanged').show('slow');
    }
</script>
<div id="divChange">
    <asp:TextBox ID="txtPhoneCell" runat="server"></asp:TextBox>
</div>
<div id="divPhoneChanged" style="display: none; padding-bottom: 15px">
    <asp:Label ID="lblPhoneChanged" runat="server" Font-Bold="true" Text="Your phone number on file will be updated with the value you provided above.">
    </asp:Label>
</div>
<div style="margin-top: 10px">
    <asp:Button ID="btnSubmit" runat="server" Text="Submit" />
</div>

并在页面后面的代码中加载:

txtPhoneCell.Attributes.Add("onchange", String.Format("phoneChanged(this, '{0}')", strPhoneCell))

2 个答案:

答案 0 :(得分:1)

我使用标准html代码编写了代码。所以没有asp.net。触发startSubmit事件时,它依赖于将全局值(false)设置为onchange。表单仅在其值为true时提交。

替代项:

  • 代替startSubmit变量,您可以定义一个隐藏输入,当触发false事件时,其值将更改为onchange
  • 您可以在.submit()元素上定义onsubmit属性,而不是使用form jquery方法。

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=yes" />
        <meta charset="UTF-8" />
        <!-- The above 3 meta tags must come first in the head -->

        <title>Demo - Prevent submit</title>

        <script src="https://code.jquery.com/jquery-1.3.2.min.js" type="text/javascript"></script>

        <script type="text/javascript">
            var startSubmit = true;

            $(document).ready(function () {
                $('#theForm').submit(function (event) {
                    if (!startSubmit) {
                        event.preventDefault();
                        startSubmit = true; // Set to "true", so that the next click submits the form.
                        return false;
                    }
                    return true;
                });
            });

            function phoneChanged(currentElement, valueToCompareAgainst) {
                $('#divPhoneChanged').show('slow');
                startSubmit = false;
            }
        </script>
    </head>
    <body>

        <!-- This random number changes on each page refresh, e.g. on each form submission. -->
        Page reloaded: <strong><?php echo rand(1, 99999); ?></strong>

        <br/><br/>

        <form id="theForm" action="" method="post">

            <div id="divChange">
                <input type="text" id="txtPhoneCell" onchange="phoneChanged(this, '4');">
            </div>

            <div id="divPhoneChanged" style="display: none; padding-bottom: 15px">
                <label id="lblPhoneChanged">
                    Your phone number on file will be updated with the value you provided above.
                </label>
            </div>

            <div style="margin-top: 10px">
                <button type="submit" id="btnSubmit">
                    Submit
                </button>
            </div>

        </form>

    </body>
</html>

编辑:

我进行了许多测试,但找不到比上述解决方案更优雅的解决方案。就个人而言,我会选择像推荐@rickjerrity那样做:

<script type="text/javascript">
    function phoneChanged(currentElement, valueToCompareAgainst) {
        $('#divPhoneChanged').show('slow');
    }
</script>

[...]

<input type="text" id="txtPhoneCell" onkeyup="phoneChanged(this, '4');">

答案 1 :(得分:0)

尝试在JavaScript中使用preventDefault函数。

Prevent Default on W3 schools

相关问题