不能从外部返回函数或方法? [英] Cannot return from outside a function or method?

查看:374
本文介绍了不能从外部返回函数或方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在开发Web应用程序时,我想执行某些验证检查,只有在经过验证之后,我需要将表单和重定向控件发布到下一页。



JavaScript代码:

 函数fnCheckEmptyField()
{
var strDomain = document.getElementsByName(txtIIDN)。
if(strDomain == null)
{
document.getElementById(lblValidityStatus)。innerHTML =;
document.getElementById(lblValidityStatus)innerHTML =域名字段不能留空;
返回false;
}
else
{
return true;
}
}

相关的HTML代码:

 < form action =Result.jspname =validityCheckonsubmit =return fnCheckEmptyField()> 
< input type =textid =txtIIDN/>
< input type =submitid =btnValidityCheckvalue =检查有效性/>
< / form>

onsubmit =return fnCheckEmptyField()显示错误无法从外部函数或方法返回,并且在执行JavaScript函数表单后,无论文本字段为空,都将提交。



我已经在中发送警报,如果条件,并且确定如果字段为空函数返回 false



我不知道我的代码有什么问题,为什么这个错误与不能从外部函数或方法返回

>

原因是什么,如何解决?

解决方案


onsubmit =return fnCheckEmptyField()显示错误无法从外部函数或方法返回


这是Eclipse特有的。 Eclipse在这里是错误的,那条线是完全正常的。只要忽略Eclipse的错误。如果需要,您可以随时禁用其JS验证。







执行java脚本函数表单正在提交,无论文本字段是否为空。


这是因为您的JavaScript功能错了你的JS代码有两个错误。


  1. getElementsByName()调用不正确。

      var strDomain = document.getElementsByName(txtIIDN)。要通过ID检索元素,您需要 getElementById()

      var strDomain = document.getElementById(txtIIDN)。 


  2. 空值不是 null ,但只是空字符串。

      if(strDomain == null)
    pre>

    您需要检查其长度。

      if strDomain.length == 0)

    或者只是使用JS布尔魔术。

      if(!strDomain)


顺便说一句, document.getElementById(lblValidityStatus)。innerHTML =; 是不必要的在这段代码中。


While developing a web application I want to perform certain validation check and only after sucesssful validation I need to post the form and redirect control to next page.

JavaScript code:

function fnCheckEmptyField()
{
    var strDomain = document.getElementsByName("txtIIDN").value;
    if (strDomain == null)
    {
        document.getElementById("lblValidityStatus").innerHTML = "";
        document.getElementById("lblValidityStatus").innerHTML = "Domain Name Field Can't be Left Blank";
        return false;
    }
    else
    {
        return true; 
    }
}

Relevant HTML code:

<form action="Result.jsp" name="validityCheck" onsubmit="return fnCheckEmptyField()">
    <input type="text" id="txtIIDN"/>
    <input type="submit" id="btnValidityCheck" value="Check Validity" />
</form>

The line onsubmit="return fnCheckEmptyField()" shows an error Cannot return from outside a function or method and after execution of the JavaScript function form is getting submitted regardless the text field is blank or not.

I have placed alerts inside if condition and it is sure that if field is empty function returns false.

I don't know what's wrong with my code and why this errors with Cannot return from outside a function or method.

What's the cause and how can I solve it?

解决方案

the line onsubmit="return fnCheckEmptyField()" showing an error Cannot return from outside a function or method

That's specific to Eclipse. Eclipse is wrong here, that line is perfectly fine. Just ignore the Eclipse error. If you want, you can always disable its JS validation.


and after execution of the java script function form is getting submitted regardless the text field is blank or not.

That's because your JavaScript function is wrong. You've 2 mistakes in your JS code.

  1. The getElementsByName() call is incorrect.

    var strDomain = document.getElementsByName("txtIIDN").value;
    

    To retrieve an element by ID, you need getElementById().

    var strDomain = document.getElementById("txtIIDN").value;
    

  2. Empty values are not null, but just empty string.

    if (strDomain == null)
    

    You need to check its length instead.

    if (strDomain.length == 0)
    

    Or just make use of JS boolean magic.

    if (!strDomain)
    

By the way, the line document.getElementById("lblValidityStatus").innerHTML = ""; is unnecessary in this code.

这篇关于不能从外部返回函数或方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆