以下JavaScript中的语法错误

时间:2012-04-02 05:44:18

标签: javascript

<script type="text/javascript">
function validate(){
    var valid = true;
     if ( document.<portlet:namespace/>fm.<portlet:namespace/>eventName.value == "" ){
         alert ( "Please Give a valid Event Name!" );
         document.<portlet:namespace/>fm.<portlet:namespace/>eventName.focus();
          valid = false;
     }
     else if ( document.<portlet:namespace/>fm.<portlet:namespace/>triggerOfEvent.value == "" ){
         document.<portlet:namespace/>fm.<portlet:namespace/>triggerOfEvent.focus();
         alert ( "Please select Trigger of Event" );
           valid = false;
     } 
     else ( document.<portlet:namespace/>fm.<portlet:namespace/>nextEvent.value == "" ){
         document.<portlet:namespace/>fm.<portlet:namespace/>nextEvent.focus();
         alert ( "Please select Trigger of Event" );
           valid = false;
     } 
    return valid;
}

</script>

此脚本显示语法错误,如返回无效,语句丢失,这些类型错误即将发生.... 我错过了任何事情......你能不能让我认识朋友.....

我正在开发liferay portlet开发....

1 个答案:

答案 0 :(得分:1)

假设您的portlet命名空间是正确的,那么您发布的代码也是正确的。

如果表单字段中存在拼写错误,则代码中会出现错误。 另外,如果portlet替换代码不起作用(请显示代码和表单的视图源),那么它将失败

我用XXX替换了portlet vars进行测试,并提出了

<script type="text/javascript">
function validate(){
    var valid = true;
     if ( document.XXXfm.XXXeventName.value == "" ){
         alert ( "Please Give a valid Event Name!" );
         document.XXXfm.XXXeventName.focus();
          valid = false;
     }
     else if ( document.XXXfm.XXXtriggerOfEvent.value == "" ){
         document.XXXfm.XXXtriggerOfEvent.focus();
         alert ( "Please select Trigger of Event" );
           valid = false;
     } 
     else ( document.XXXfm.XXXnextEvent.value == "" ){
         document.XXXfm.XXXnextEvent.focus();
         alert ( "Please select Trigger of Event" );
           valid = false;
     } 
    return valid;
}

</script>

这是有效的代码。

我重写它更简单,并通过传递表单对象来避免表单名称:

<form onsubmit="return validate(this)">

DEMO

function validate(theForm){
     if (theForm.XXXeventName.value == "" ){
         alert ( "Please Give a valid Event Name!" );
         theForm.XXXeventName.focus();
         return false;
     }
     if (theForm.XXXtriggerOfEvent.value == "" ){
         theForm.XXXtriggerOfEvent.focus();
         alert ( "Please select Trigger of Event" );
         return false;
     }
     if (theForm.XXXnextEvent.value == "" ){
         theForm.XXXnextEvent.focus();
         alert ( "Please select Trigger of Event" );
         return false;
     }
    return true;
}