我有这个代码。我为它的混乱道歉。我是新手,因此出现了问题。它应该从查询字符串中填充复选框。它适用于 Chrome、Safari、Firefox。刚开始在 IE 上测试,在 IE 8 中它不起作用。点击已注册,但未填充复选框。没有错误被抛出,它似乎只是忽略它。
//set var for queryString - "?"
var queryString = window.location.search.substring(1);
var parameters = queryString.split('&');
var paramObject = new Object();
for( var i =0; i < parameters.length; ++i){
var equalsPos = parameters[i].indexOf('=');
var key = decodeURIComponent(parameters[i].substring(0,equalsPos));
var stringLength = parameters[i].length;
var value = decodeURIComponent(parameters[i].substring(equalsPos + 1, stringLength).replace(/\+/g,' '));
if(!paramObject[key]) {
//console.log('paramObject[key] =' + paramObject[key]);
paramObject[key] = value;
//console.log('paramObject[key] = value = ' + value + paramObject[key]);
//console.log(paramObject[key]);
}else if(paramObject[key] instanceof Array){
paramObject[key].push(value);
//console.log(paramObject[key]);
} else {
var newArray = [];
var existingValue = paramObject[key];
//console.log('existing value: '+ existingValue);
//console.log('value: ' + value);
newArray.push(existingValue);
newArray.push(value);
paramObject[key] = newArray;
}
}
//console.log(paramObject);
for (key in paramObject) {
$('input[name="' + key + '"]').each(function(){
var obj = paramObject[key];
for(prop in obj){
if(obj instanceof Array){
if(obj.hasOwnProperty(prop)){
//console.log(key + '=' + obj[prop]);
if($(this).attr('value') == obj[prop]){
$(this).attr('checked','checked');
}
}
}else{
if(obj.hasOwnProperty(prop)){
if($(this).attr('value') == obj){
$(this).attr('checked','checked');
}
}
}
}
});
}