小弟前天遇到一个奇怪的问题......困扰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 逻辑上本可以写的更好点但当我用其他逻辑时,也是直接报错。现在这个版本是在不报错的基础上写的比较好的版本。
我现在这个工程是用 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 逻辑上本可以写的更好点但当我用其他逻辑时,也是直接报错。现在这个版本是在不报错的基础上写的比较好的版本。
不过这个null 我是想保留的这样每次这个弹出页面只做一次从 window.parent.document 取数据
(如果 js 里面 null 和 "" 不一样的话)
直接也赋值为Null不行吗?
真正 submit 的时候做的是 showCountriesUnderZone(),这时候才把 selectCheckBox 得到的结果放到之前的页面上去
所以如果直接为Null的话逻辑上会再从之前页面上取得值
if(countryListUnderZone == null){
countryListUnderZone = window.parent.document.getElementById(zoneId + exOrIm + 'Country').innerHTML;
}
但是如果用户只点了 checkBox 但是没有按 submit 提交的话,会出问题......
所以只能是 "", 不能是 null .
或者改之前的逻辑。
像你这么写,countryListUnderZone 又要存值,又要去判断是否选择,难免会造成逻辑上的混乱.
问个问题如果你定义 var temp = null;
那之后对这个 temp 的赋值是不是理论上什么类型都可以的?
temp = "123";
temp = false;
temp = 0;
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 = ""; 就会报错一赋值就会报错
定义时: var countryListUnderZone = "";
是可以得。但是在:
if(countries.length == 0){
countryListUnderZone ="";
return;
}加上上面的蓝色部分,就会出错
真正 submit 的时候做的是 showCountriesUnderZone(),这时候才把 selectCheckBox 得到的结果放到之前的页面上去
所以如果直接为Null的话逻辑上会再从之前页面上取得值
if(countryListUnderZone == null){
countryListUnderZone = window.parent.document.getElementById(zoneId + exOrIm + 'Country').innerHTML;
}
但是如果用户只点了 checkBox 但是没有按 submit 提交的话,会出问题......
所以只能是 "", 不能是 null .
或者改之前的逻辑。