簡體   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