Javascript不应该被触发

时间:2011-07-15 10:04:12

标签: php javascript html forms

提交表单时,如何触发Javascript isValidEmailAndEqual:

echo("<script language='javascript' type='text/javascript'>
<!--

function isValidEmailAndEqual() {
regExp = /^[^@]+@[^@]+$/;


if((document.subscribe.email1.value.search(regExp) == -1) || !isEqual(document.subscribe.email1.value, document.subscribe.email2.value)) 
   { 
   //alert(isEqual(document.subscribe.email1.value, document.subscribe.email2.value))
       alert('Incorrect entered email addresses. They must be valid e-mail addresses and equal.');
      return false; 
   } 

   //document.subscribeForm.submit();

return true; 
}


-->
</script>");

echo ("<div class='subscribe'>
     <h4>Subscribe</h4>
     <p>Subscribe to iAndApp's newsletter in order to get information about new and updated iPhone games and iPhone applications, that has been released by iAndApp. </p>
     <form action='subscription.php' name='subscribe' method='post'  onsubmit='return isValidEmailAndEqual()'>
        <p class='formlabel'>Förnamn</p> <input type='text' name='first_name'/><br/>
        <p class='formlabel'>Efternamn</p> <input type='text' name='surname'/> <br/>
        <p class='formlabel'>E-mail</p> <input type='text' name='email1'/><br/>
        <p class='formlabel'>Repeat e-mail</p> <input type='text' name='email2'/> <br/>
        <input class='inputsubmit' type='submit' value='Subscribe'/>
     </form>
</div>");

Firebug告诉我页面上没有javascript。

我已尝试使用和不使用注释,并为结束注释标记添加前缀。

我所做的,可能很重要,是从服务器加载以下内容,而不是添加到div标签。内容已呈现,但脚本仍无法正常工作。我还尝试在主/父页面上添加脚本(页面加载内容并将其添加到div),其中我有其他可用的脚本(尽管由主页/父页面中的组件触发)。

提前谢谢!

2 个答案:

答案 0 :(得分:1)

应该是
echo("<script language='javascript' type='text/javascript'>
<!--
//your code here, obviously not commented
//-->
</script>

仅供参考,http://www.w3.org/TR/html4/interact/scripts.html#h-18.3.2

答案 1 :(得分:0)

实际上,它们被触发了。以下HTML(完全由您的脚本输出)导致该函数在firefox 5和chrome中触发。作为旁注,您可能不需要html评论(<!---->)。这种做法通常不再需要,因为不支持javascript的浏览器会忽略标记内的所有内容。

如果您必须拥有html注释,请注释掉-->以避免触发JS错误(如Kumar所解释的那样)。

<script language='javascript' type='text/javascript'> 
<!--

function isValidEmailAndEqual() {
regExp = /^[^@]+@[^@]+$/;


if((document.subscribe.email1.value.search(regExp) == -1) || !isEqual(document.subscribe.email1.value, document.subscribe.email2.value)) 
   { 
   //alert(isEqual(document.subscribe.email1.value, document.subscribe.email2.value))
       alert('Incorrect entered email addresses. They must be valid e-mail addresses and equal.');
      return false; 
   } 

   //document.subscribeForm.submit();

return true; 
}


-->
</script><div class='subscribe'> 
     <h4>Subscribe</h4> 
     <p>Subscribe to iAndApp's newsletter in order to get information about new and updated iPhone games and iPhone applications, that has been released by iAndApp. </p> 
     <form action='subscription.php' name='subscribe' method='post'  onsubmit='return isValidEmailAndEqual()'> 
        <p class='formlabel'>Förnamn</p> <input type='text' name='first_name'/><br/> 
        <p class='formlabel'>Efternamn</p> <input type='text' name='surname'/> <br/> 
        <p class='formlabel'>E-mail</p> <input type='text' name='email1'/><br/> 
        <p class='formlabel'>Repeat e-mail</p> <input type='text' name='email2'/> <br/> 
        <input class='inputsubmit' type='submit' value='Subscribe'/> 
     </form> 
</div>