繁体   English   中英

Javascript无法运行

[英]Javascript does not run

当用户按下“提交”按钮并且输入框中没有文本时,我希望显示一个错误窗口。 我正在使用JSF创建网页和脚本的JS。 由于某些原因,脚本无法运行。

这是整个页面代码:

    <?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:p="http://primefaces.org/ui"
      > 
    <link rel="stylesheet" href="testcss.css" />


    <h:head>
        <title>Print to Uni</title>


        <meta http-equiv="content-type" content="text/html; charset=windows-1252" />
        <link rel="stylesheet" type="text/css" href="style.css" title="style" />
    </h:head>

    <body>
        <div id="main">
            <div id="header">

                <div id="logo">
                    <div id="logo_text">
                        <h1><a href="index.xhtml">Remote<span class="logo_colour">Printing</span></a></h1>
                        <h2>This is a web app that allows users to print to University</h2>
                    </div>
                </div>
            </div>

        </div>
    </body>
    <div id="site_content">
        <div id="content">
            <h:body>


                <h:form onsubmit="return required();">

                    Please enter your username:
                    <h:inputText id="Username" value="#{user.id}"
                                 size="20" required="true" label="Username">

                    </h:inputText><br></br><br></br>

                    To print a piece of text, please press the submit button below to upload the text:<br/><br/>
                    <h:commandButton type="Submit" value="Submit" onclick="return username_validation()" action="upload/uploadText" styleClass="Submit"/>

                </h:form>

            </h:body>
        </div>
    </div>
    <script type="text/javascript"> 


        function username_validation(){  //cannot get this to work
            if (document.getElementById("Username").value.length == 1) 
            {   
                alert("Please enter a username");        
                return false;   
            }       
            return true;   
        }

        function show_alert() { 
            var msg = "Thank you";
            alert(msg); 
        }

    </script> 
</html>

这是我在用户按下Submit时试图运行的代码:

 function username_validation(){  //cannot get this to work
        if (document.getElementById("Username").value.length == 1) 
        {   
            alert("Please enter a username");        
            return false;   
        }       
        return true;  

和继承人的形式:

            Please enter your username:
            <h:inputText id="Username" value="#{user.id}"
                         size="20" required="true" label="Username">

            </h:inputText><br></br><br></br>

            To print a piece of text, please press the submit button below to upload the text:<br/><br/>
            <h:commandButton type="Submit" value="Submit" onclick="return username_validation()" action="upload/uploadText" styleClass="Submit"/>

        </h:form>

问题是这一行:

if (document.getElementById("Username").value.length == 1) 

仅在用户名恰好一个字符长的情况下才会失败。

如果要验证用户名是否存在,请执行以下操作:

if (document.getElementById("Username").value.length < 1) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM