eclipse 无法从函数或方法外部返回?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8924335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot return from outside a function or method?
提问by Nishit Jain
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.
在开发 Web 应用程序时,我想执行某些验证检查,只有在成功验证后,我才需要发布表单并将控件重定向到下一页。
JavaScript code:
JavaScript 代码:
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:
相关HTML代码:
<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 methodand after execution of the JavaScript function form is getting submitted regardless the text field is blank or not.
该行onsubmit="return fnCheckEmptyField()"
显示错误无法从函数或方法外部返回,并且在执行 JavaScript 函数后,无论文本字段是否为空,表单都会提交。
I have placed alerts inside if
condition and it is sure that if field is empty function returns false
.
我在if
条件中放置了警报,并且确定如果字段为空函数返回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?
是什么原因,我该如何解决?
回答by BalusC
the line
onsubmit="return fnCheckEmptyField()"
showing an error Cannot return from outside a function or method
onsubmit="return fnCheckEmptyField()"
显示错误的行无法从函数或方法外部返回
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.
这是 Eclipse 特有的。Eclipse 在这里是错误的,那条线完全没问题。只需忽略 Eclipse 错误。如果需要,您可以随时禁用其 JS 验证。
and after execution of the java script function form is getting submitted regardless the text field is blank or not.
并且在执行java脚本函数后,无论文本字段是否为空,表单都会被提交。
That's because your JavaScript function is wrong. You've 2 mistakes in your JS code.
那是因为你的 JavaScript 函数是错误的。您的 JS 代码中有 2 个错误。
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;
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)
该
getElementsByName()
调用不正确。var strDomain = document.getElementsByName("txtIIDN").value;
要按 ID 检索元素,您需要
getElementById()
.var strDomain = document.getElementById("txtIIDN").value;
空值不是
null
,而只是空字符串。if (strDomain == null)
您需要检查它的长度。
if (strDomain.length == 0)
或者只是利用 JS 布尔魔法。
if (!strDomain)
By the way, the line document.getElementById("lblValidityStatus").innerHTML = "";
is unnecessary in this code.
顺便说一下,document.getElementById("lblValidityStatus").innerHTML = "";
这段代码中不需要该行。
回答by under5hell
remove the "return" from onsubmit attribut your code should look like this
从 onsubmit 属性中删除“返回”,您的代码应如下所示
<form action="Result.jsp" name="validityCheck" onsubmit="fnCheckEmptyField()">
<input type="text" id="txtIIDN"/>
</form>
hope this solve your problem :-)
希望这能解决您的问题:-)
回答by David Levy
Also,
还,
var strDomain= document.getElementsByName("txtIIDN").value;
should be
应该
var strDomain= document.getElementById("txtIIDN").value;
The text field has an id, not a name
文本字段有一个 id,而不是一个名字
回答by Kishor
Remove Action from Form Tag.
从表单标签中删除操作。
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";
}
else
{
window.navigate("top.jsp");
}
}
回答by fredo
I'm no javascript expert but I think you should lose the return keyword in your onsubmit
parameter in the HTML; like below:
我不是 javascript 专家,但我认为您应该onsubmit
在 HTML的参数中丢失 return 关键字;像下面这样:
onsubmit="fnCheckEmptyField()"
edit: sorry was posting concurrently with previous answer
编辑:抱歉与之前的答案同时发布