不执行 if 比较

An if comparison is not executed

本文关键字:比较 if 执行      更新时间:2023-12-28

我有以下脚本:

<script type="text/javascript" language="javascript">
    function ValMail() {
        var txt = document.getElementById("<%= Email.ClientID%>");
        "<%mailFormat(Email.Text)%>"
        if ('<%= mType%>' != 'false') {
            txt.value = '';
            txt.focus();
        }
    }
</script>

我从 asp.net 页面控件调用它:

<asp:TextBox  ID="Email" runat="server" TabIndex="4" CssClass="textEntry" 
    CausesValidation="True" OnClientBlur="ValMail()" AutoPostBack="True">
</asp:TextBox>

调用脚本的OnClientBlur很好(我接下来是调试(出现在if行中,但没有更进一步。

mType是在代码后面声明的Public Shared;并且非常确定它具有false的值。我可以在这方面得到帮助吗?
代码始终从'<%= mType%>'中消除

更新:建议的解决方案是使用 RequiredFieldValidator 和 RegularExpressionValidator 来验证电子邮件地址,这是更适合 ASP.NET WebForms 验证的方法。原始代码中的基本缺陷是,它使用服务器生成的值(而不是用户输入的实际电子邮件,总是被忽略(在客户端测试电子邮件地址的有效性。/更新结束

也许您可以将渲染的javascript粘贴到此处,而无需服务器端逻辑。只有浏览器看到的内容。

另外,这行是否有可能:

    "<%mailFormat(Email.Text)%>"

是否缺少尾随分号?

您必须编写一个 .net 变量(data( 作为文字才能像 javascript 变量一样使用它。试试这个

使用mType使用mType.toString() .看到这里

    var boolVar = '<%= mType.toString()%>';
    // alert(boolVar);
    if (boolVar != 'false')
    {
        txt.value = '';
        txt.focus();
    }