当我把代码写这样,表单中不填写任何值的时候是可以运行的。
if(document.geElementById('name').value==""){
alert ('error');
}
但是如果我改成这样,就不能正确运行。
if(document.geElementById('name').value== null ){
alert ('error');
}
这是因为DOM获取的问题,还是javascript中空值定义与其他语言不一样?
寻人回答下这个 问题。

解决方案 »

  1.   

    document.geElementById('name').value 返回的是 string 
    string没有为null的
      

  2.   

    你其实这样就行了:alert(typeof(document.getElementById('name').value));无论你以后做什么判断,可以弹一下他的类型,typeof 就是弹出后面指定对象的类型。这样弹一下就知道是 string 还是 其他什么的。这里明显弹出的是 string,因此判断是用JS里的 '' 代表空字符串。
      

  3.   

    可能是跟 name 这个有关建议把文本框修改为 user_name 或 title 这样子的。$var user_name=document.geElementById('user_name').value;
    if(user_name==""||empty(user_name)){
    alert ('error');
    }试试~