学习:return 使用

function gtCity(val) {
  baidu.ajax.post("http://fe.baidu.com/dev/tangram/assets/ajaxRequest.php", "x=" + val,
  function(xhr, responseText) {
    if (xhr.status == 200) {
      if (val == "") {} else {
        alert("状态" + xhr.status + "请求成功" + responseText);
      }
    } else {
      alert("传输错误")
    }
  });
}

//说明:如果为空不执行任何函数

 

更好的写法(先声明)

var gtCity = function(val){

  if(val==""){

    return;

  }

  baidu.ajax.post("http://fe.baidu.com/dev/tangram/assets/ajaxRequest.php", "x=" + val,
  function(xhr, responseText) {
    if (xhr.status == 200) {

        alert("状态" + xhr.status + "请求成功" + responseText);
    } else {
      alert("传输错误")
    }
  });
}

posted @ 2010-12-04 18:47  Younger  阅读(185)  评论(0)    收藏  举报