小弟前天遇到一个奇怪的问题......困扰2天了,实在没招..上来求教下
我现在这个工程是用 tapestry 做的,不过问题是出在 js 上
页面上的代码如下:
<script type="text/javascript">
window.onload = function() {
if ($('close').value == 'true') {
parent.Control.Modal.close();
}
}

var countryListUnderZone = null;
var zoneId;
var exOrIm;
function selectCheckBox(countryName){
zoneId = document.getElementById('zoneId').value;
exOrIm = document.getElementById('exOrIm').value;
if(countryListUnderZone == null){
countryListUnderZone = window.parent.document.getElementById(zoneId + exOrIm + 'Country').innerHTML;
}
var countries = trim(countryListUnderZone).split(' - ');
var temp = 0;
for(i = 0; i < countries.length; i++){
if(countries[i] == countryName){
countries.splice(i, 1);
temp = 1;
break;
}
} if(temp == 0 && !(countries.length == 0)){
countries.splice(countries.length, 0, countryName);
}

var tempCountriesString;
for(i = 0; i < countries.length; i++){
tempCountriesString = tempCountriesString + " - " + countries[i];
}

if(countries.length == 0){
countryListUnderZone = "";
return;
}

countryListUnderZone = tempCountriesString.substring(12);

} function trim(str) {
return str.replace(/^\s+|\s+$/g, "");
} function showCountriesUnderZone(){
if(countryListUnderZone != null){
window.parent.document.getElementById(zoneId + exOrIm + 'Country').innerHTML = countryListUnderZone;
}
} </script>这个页面是按了一个link 之后,弹出一个页面,在这个页面上 onclick 调用 selectCheckBox 方法后,在不刷页面的情况下,改变之前页面的显示。
逻辑上,通过 window.parent.document.getElementById() 来改变。
问题出在 红字 的地方, 当我写成:
countryListUnderZone;
一切正常, 但当我写成:
countryListUnderZone = "";
时,弹出的页面直接报错, 貌似是 解析文件的时候出错。(这个方法是 onclick 才做的)小弟知道 光看 js 可能不能解决这个问题不过东西实在太多,不能全贴出来
本人分不多,只求,哪位可以给点提示给我个方向
P.S 逻辑上本可以写的更好点但当我用其他逻辑时,也是直接报错。现在这个版本是在不报错的基础上写的比较好的版本。

解决方案 »

  1.   

    var countryListUnderZone = null; 改成 var countryListUnderZone;试试..
      

  2.   

    谢谢楼上回复的这么快
    不过这个null 我是想保留的这样每次这个弹出页面只做一次从 window.parent.document 取数据
    (如果 js 里面 null 和 "" 不一样的话)
      

  3.   

    那你为什么要将 countryListUnderZone = ""; 
    直接也赋值为Null不行吗?
      

  4.   

    selectCheckBox  是在点了页面上的 checkBox 时候做的
    真正 submit 的时候做的是 showCountriesUnderZone(),这时候才把 selectCheckBox 得到的结果放到之前的页面上去
    所以如果直接为Null的话逻辑上会再从之前页面上取得值
    if(countryListUnderZone == null){
        countryListUnderZone = window.parent.document.getElementById(zoneId + exOrIm + 'Country').innerHTML;

    但是如果用户只点了 checkBox 但是没有按 submit 提交的话,会出问题......
    所以只能是 "", 不能是 null .
    或者改之前的逻辑。
      

  5.   

    建议你设置一个标志变量..
    像你这么写,countryListUnderZone 又要存值,又要去判断是否选择,难免会造成逻辑上的混乱.
      

  6.   

    xiaoqijun 还在不?
    问个问题如果你定义 var temp = null;
    那之后对这个 temp 的赋值是不是理论上什么类型都可以的?
    temp = "123";
    temp = false;
    temp = 0;
      

  7.   

    刚才尝试了下,定义了一个新的变量去控制
    var haveGetCountryListOnPage = "";
    var countryListUnderZone = null;
    var zoneId;
    var exOrIm;
    function selectCheckBox(countryName){
        zoneId = document.getElementById('zoneId').value;
        exOrIm = document.getElementById('exOrIm').value; 
        if(haveGetCountryListOnPage == ""){
    countryListUnderZone = window.parent.document.getElementById(zoneId + exOrIm + 'Country').innerHTML;
    haveGetCountryListOnPage = "haveDone";
        }    //////////////////////
         未修改代码段
        //////////////////////    if(countries.length == 0){
    countryListUnderZone = null;
    return;
        }
        
    }countryListUnderZone;
    只要在方法内一给它赋值就会报错在定义时 countryListUnderZone = null; 不会报错 
    但是只要在方法中 加上  countryListUnderZone = null;  就会报错在定义时 countryListUnderZone = ""; 不会报错 
    但是只要在方法中 加上  countryListUnderZone = "";  就会报错一赋值就会报错
      

  8.   

    我觉得楼主的问题出在空值和null上面,建议楼主把null全部变成空值,然后判断countryListUnderZone是否为空,null是object手册上是这样说null的包含 null 的变量包含“无值”或“无对象”。换句话说,该变量没有保存有效的数、字符串、boolean、数组或对象。可以通过给一个变量赋 null 值来清除变量的内容。请注意,在 Jscript 中,null 与 0 不相等(与在 C 和 C++ 中不同)。同时应该指出的是,Jscript中 typeof 运算符将报告 null 值为 Object 类型,而非类型 null。这点潜在的混淆是为了向下兼容。
      

  9.   

    回楼上的
    定义时: var countryListUnderZone = ""; 
    是可以得。但是在: 
    if(countries.length == 0){
        countryListUnderZone ="";
        return;
    }加上上面的蓝色部分,就会出错
      

  10.   

    selectCheckBox  是在点了页面上的 checkBox 时候做的 
    真正 submit 的时候做的是 showCountriesUnderZone(),这时候才把 selectCheckBox 得到的结果放到之前的页面上去 
    所以如果直接为Null的话逻辑上会再从之前页面上取得值 
    if(countryListUnderZone == null){ 
        countryListUnderZone = window.parent.document.getElementById(zoneId + exOrIm + 'Country').innerHTML; 

    但是如果用户只点了 checkBox 但是没有按 submit 提交的话,会出问题...... 
    所以只能是 "", 不能是 null . 
    或者改之前的逻辑。
      

  11.   

    可能是countryListUnderZone 这个命名和其他对象重名