在这里返回真假有什么用?

时间:2012-08-31 07:39:50

标签: javascript html dom

function userid_validation(uid, mx, my) {
  var uid_len = uid.value.length;

  if (uid_len == 0 || uid_len >= my || uid_len < mx) {
    alert("User Id should not be empty / length be between " + mx + " to " + my);
    uid.focus(); //is this focus will be on alert box?  
    return false; //what is use of this false ?
  }

  return true; //what is use of this true?
}

2 个答案:

答案 0 :(得分:0)

如果没有看到调用代码的位置,就无法完全确定......但很可能是从DOM0事件处理程序调用(由<element>.onclick=分配)

false会阻止默认操作发生,而true会允许它。

function userid_validation(uid,mx,my)
{
  var uid_len = uid.value.length;
  if (uid_len == 0 || uid_len >= my || uid_len < mx)
  {
    alert("User Id should not be empty / length be between "+mx+" to "+my);
    uid.focus(); //is this focus will be on alert box?
    return false; //what is use of this false ? 
  }
return true; //what is use of this true? 
}

答案 1 :(得分:0)

我想它可以防止事件发生。通常的做法是阻止链接和表单的标准行为(防止重定向和提交)。