日期:2014-05-16  浏览次数:20302 次

关于javascript的判断语句问题
本帖最后由 spyq_1992 于 2013-11-24 06:43:42 编辑
function selectUser(userId){
    var e=document.getElementById("a");
//通过id找到input的位置
//    e.value=e.value+e.value.indexOf(userId);
    //上面语句显示,e.value.indexOf(userId)的值为-1
    
    if( e.value.indexOf(userId) == -1){
        //看是不是input里面已经含有该用户id
        e.value=e.value+userId+',';
        //如果没有含有,那么在input位置加上新的id和逗号
}
else{
    e.value.replace(userId+",",""));  
        //如果input已经含有了该id那么就把这个id从input里面除去
}  
}


上面的代码在网页显示的时候无法跳入if语句,里面的expression也就无法运行,但是我把if语句删掉就可以运行了,求助各位大神!!
javascript

------解决方案--------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
function selectUser(userId){
    var e=document.getElementById("a");
//通过id找到input的位置
//    e.value=e.value+e.value.indexOf(userId);
    //上面语句显示,e.value.indexOf(userId)的值为-1
     
    if( e.value.indexOf(userId) == -1){
        //看是不是input里面已经含有该用户id
        e.value=e.value+userId+',';
        //如果没有含有,那么在input位置加上新的id和逗号
    }else{
        e.value.replace(userId+",","");  
        //如果input已经含有了该id那么就把这个id从input里面除去
    }     
}
</script>
</head>

<body>
<input type="text" onblur="selectUser('a')" id='a'/>
</body>
</html>
else判断里多了个)