我有简单的JavaScript函数,它根据一些数据执行一个简单的字符串。我使用的JavaScript功能返回false在javascript中不工作
function checkdata() {
var data = document.getElementById('data').value;
if (data != '') {
console.log("Hello");
return false;
} else {
return true;
}
}
function gotothen() {
console.log("Bye");
}
function changemode(value) {
if (value == 0) {
checkdata();
gotothen();
}
if (value == 1) {
checkdata();
gotothen();
}
}
,我使用的HTML代码是
<input type="text" name="data" id="data">
<a href="javacript:void(0)" onclick="changemode(0)">Hotel</a>
<a href="javacript:void(1)" onclick="changemode(1)">Flight</a>
所以,如果我输入任何值的文本框,点击任何超链接调用changemode()
功能。有两个功能,我打电话changemode()
功能。一个是checkdata()
,另一个是gotothen()
。因此,如果文本框的值不是空白,那么它不应该调用gotothen()
函数,但它正在调用。它应该在打印“Hello”后停止,但在使用返回false后它也会调用gotothen()
函数。那么如果文本框的值不是空白,我怎样才能停止执行gotothen()
函数?
你''从checkdata return'值( )'。这不会***自动'返回'并中止'changemode()'中的代码。你不会对'checkdata'的返回值做任何事情。你直接调用'checkdata'然后*无条件地调用'gotothen';每一次。 – deceze
所以基本上返回false将无法在这种情况下工作? –