2012-12-24 94 views
-1

当用户按下提交按钮并且输入框中没有文本时,我想要出现一个错误窗口。我正在使用JSF为脚本创建网页和JS。由于某种原因,脚本无法运行。Javascript does not run

这里是整个页面代码:

<?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> 

这是我试图运行时,用户按下提交代码:

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

和继承人的hform:

  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> 
+2

Web浏览器不明白''。你能展示浏览器得到什么吗? –

+0

“required”功能位于何处?我无法在任何地方看到它。 –

+0

如果''编译为'',则不应将其放在之前的任何其他''中,也不应该放在''之前。 –

回答

4

问题是这条线:

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

只有当用户名只有一个字符长时才会失败。

如果您想验证用户名是不存在的,而不是以下:

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

onclick =“return username_validation();” – HMarioD

+0

'Error解析/index.xhtml:错误跟踪[line:59]元素的内容必须包含格式正确的字符数据或标记.'当我尝试<1 – user1924104

+0

谢谢我已添加onlick = ....但仍然不是运气,用我当前的代码更新原始问题 – user1924104