使用javascript

时间:2016-03-27 10:08:39

标签: javascript html xslt

我被困在一段代码中。当我使用xsl时,我无法验证表单。但同样适用于html。

它真的很奇怪,我不知道解决方案。

我的HTML代码完美无缺。

<body>
<form name="myform" method="post" action="http://www.google.com" onsubmit="return validateform()" >
Name: <input type="text" name="name"><br/>
Password: <input type="password" name="password"><br/>
<input type="submit" value="register">
</form>
</body>

我的XSL代码,无法验证并直接到达网址。

<xsl:element name="form">
                  <xsl:attribute name="name">myform</xsl:attribute>
                  <xsl:attribute name="method">post</xsl:attribute>
                  <xsl:attribute name="action">http://www.google.com</xsl:attribute>
                  <xsl:attribute name="onsubmit">return validateform();</xsl:attribute>

                  <xsl:element name="input">
                    <xsl:attribute name="type">text</xsl:attribute>
                    <xsl:attribute name="name">name</xsl:attribute>
                  </xsl:element>

                  <xsl:element name="input">
                    <xsl:attribute name="type">password</xsl:attribute>
                    <xsl:attribute name="name">password</xsl:attribute>
                  </xsl:element>

                  <xsl:element name="input">
                    <xsl:attribute name="type">submit</xsl:attribute>
                    <xsl:attribute name="value">register</xsl:attribute>
                  </xsl:element>

           </xsl:element>

MY JS两者相同

<script>
function validateform(){
 var name=document.myform.name.value;
 var password=document.myform.password.value;
 if (name==null || name==""){
  alert("Name can't be blank");  
  return false;  
 }
 else if(password.length<6){  
 alert("Password must be at least 6 characters long.");  
 return false;  
 }  
}
</script> 

单击注册“未读取属性”值未定义后,我的控制台出错。

P.S:我不想使用必需的属性,因为它不适用于IE,我不想使用jQuery。

1 个答案:

答案 0 :(得分:0)

这应该有效,但你遗漏了一些信息 脚本部分是否也是通过xslt生成的?如何调用xslt处理器?在浏览器中?

但是如果脚本部分是通过xslt和xslt文件的已发布部分生成的,那么可能会有一个提示。这是无效的XML(XSLT)。 你需要转义一些字符(&amp;,&lt;)。 在你的情况下改变:

    <?xml version="1.0" encoding="UTF-8"?>
<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/"><soap-env:Header><eb:MessageHeader xmlns:eb="http://www.ebxml.org/namespaces/messageHeader" eb:version="1.0" soap-env:mustUnderstand="1"><eb:From><eb:PartyId eb:type="urn:x12.org:IO5:01">sws3-crt.cert.sabre.com</eb:PartyId></eb:From><eb:To><eb:PartyId eb:type="urn:x12.org:IO5:01">7973</eb:PartyId></eb:To><eb:CPAId>OH88</eb:CPAId><eb:ConversationId>y4JiO-0PTQn5fm5SRTPz-1459075755</eb:ConversationId><eb:Service eb:type="sabreXML"/><eb:Action>HotelRateDescriptionLLSRS</eb:Action><eb:MessageData><eb:MessageId>41ade332-41f5-4d1e-ad9a-c9b8d91a8817@176</eb:MessageId><eb:Timestamp>2016-03-27T10:50:32</eb:Timestamp></eb:MessageData></eb:MessageHeader><wsse:Security xmlns:wsse="http://schemas.xmlsoap.org/ws/2002/12/secext"><wsse:BinarySecurityToken valueType="String" EncodingType="wsse:Base64Binary">Shared/IDL:IceSess\/SessMgr:1\.0.IDL/Common/!ICESMS\/CERTG!ICESMSLB\/CRT.LB!-3422351642743054173!493550!0</wsse:BinarySecurityToken></wsse:Security></soap-env:Header><soap-env:Body><HotelRateDescriptionRS xmlns="http://webservices.sabre.com/sabreXML/2011/10" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:stl="http://services.sabre.com/STL/v01" Version="2.3.0">
 <stl:ApplicationResults status="NotProcessed">
  <stl:Error type="BusinessLogic" timeStamp="2016-03-27T05:50:32-05:00">
   <stl:SystemSpecificResults>
    <stl:Message>1VERIFY FORMAT                                                 </stl:Message>
    <stl:ShortText>ERR.SWS.HOST.ERROR_IN_RESPONSE</stl:ShortText>
   </stl:SystemSpecificResults>
  </stl:Error>
 </stl:ApplicationResults>
</HotelRateDescriptionRS></soap-env:Body></soap-env:Envelope>

else if(password.length<6){  

更新

如果有多个具有相同名称的表单。更改您的validateform函数表单

 else if(password.length &lt; 6){  

To(使用表单作为参数):

 function validateform(){
   var name=document.myform.name.value;
   //...
 }

并使用它:function validateform(form){ var name= form.elements['name'].value; //... }