我希望创建是从4个选择菜单所选择的选项衍生的JSON对象意想不到非空白字符。这些菜单可能有加载时选择的选项(由于服务器端技术)或可能没有选择任何选项! “:意想不到的非空白字符后JSON数据JSON.parse”JSON.parse:后JSON数据
我一旦页面使用$(文件)。就绪()我的脚本运行...但是我得到一些问题,JSON对象加载希望我的JSON有以下结构
selObj = {
category: selectedCategory, // we can only have 1 category, this isn’t giving me a problem…
catOptions:{
optionValue:discountValue, // we can have many of these
optionValue:discountValue,
optionValue:discountValue
},
tariff:{
tariffValue:discountValue, // we can have many of these
tariffValue:discountValue
},
tarOptions:{
tarOption:discountValue, // we can have many of these
}
};
好了,这里是我的功能,我已经删除了一些项目在这里简化,但我路过原来的空物体插入函数作为参数,然后希望能修改这是因为如果/当选择菜单被更改时,我想要将JSON对象传递回服务器......但现在我们只是处理页面加载...该函数还显示了e值或在使用名为defaultTable()
的函数创建的动态表中选择的内容只是忽略了现在,它是我感兴趣的JSON对象的填充(并且遇到问题)。
var selectMenuArray = ["cat", "catOpt", "tariff", "tariffOpt"], // these are the IDs of the select menus...
selObj = {};
function setUpTable(selectArray, theObj){
// okay, if we enter the page and and the user already has a tarif selected (so it is shown in the menus) we wish to show the table with the
// default/original values...
var currentSelect,
catA = [],
catOptA = [],
tarA = [],
tarOptA = [],
catOptForObj,
tariffForObj,
tariffOptForObj,
temp1 = {},
temp2 = {},
temp3 = {},
i;
// loop through the 4 menus and see what is selected
for(i=0;i<selectArray.length;i++){
// let's look at the options to see if any are selected by looping through the <option>
currentSelect = document.getElementById(selectArray[i]);
for(j=0;j<currentSelect.length;j++){
// do we have an options with the selected attribute?
if(currentSelect.options[j].selected == true){
// okay, we need to make sure the table is shown then the correct values are shown?
// we know what the Menu is... selectArray[i], and this is the text... currentSelect.options[j].
// lets build up whet's selected in Arrays...
switch(selectArray[i]){
case "cat":
catA.push(currentSelect.options[j].value);
break;
case "catOpt":
catOptA.push(currentSelect.options[j].value);
catOptForObj = catOptForObj + '"' + currentSelect.options[j].value + '":"' + "% to come later" + '",';
break;
case "tariff":
tarA.push(currentSelect.options[j].value);
tariffForObj = tariffForObj + '"' + currentSelect.options[j].value + '":"' + "% to come later" + '",';
break;
case "tariffOpt":
tarOptA.push(currentSelect.options[j].value);
tariffOptForObj = tariffOptForObj + '"' + currentSelect.options[j].value + '":"' + "% to come later" + '",';
break;
default:
// no default?
}
}
}
}
// now we can build the table
if(catA.length > 0 || catOptA.length > 0 || tarA.length > 0 || tarOptA.length > 0){
// show table...
$("#dynamicTableHolder table").css("visibility","visible").hide().fadeIn('slow');
for(i=0;i<selectArray.length;i++){
switch(selectArray[i]){
case "cat":
defaultTable(catA, "dtCats");
theObj.cat = catA[0];
break;
case "catOpt":
defaultTable(catOptA, "dtTariffs");
temp1 = '"{' + catOptForObj.substring(0, catOptForObj.length-1).replace(/undefined/g, "") + '}"';
theObj = jQuery.parseJSON('"catOptions":' + temp1);
break;
case "tariff":
defaultTable(tarA, "dtCatOpts");
temp2 = "{" + tariffForObj.substring(0, tariffForObj.length-1).replace(/undefined/g, "") + "}";
//theObj = jQuery.parseJSON('"tariff":' + temp2);
break;
case "tariffOpt":
defaultTable(tarOptA, "dtTariffOpts");
temp3 = "{" + tariffOptForObj.substring(0, tariffOptForObj.length-1).replace(/undefined/g, "") + "}";
//theObj = jQuery.parseJSON('"tarOptions":' + temp3);
break;
default:
// no default?
}
}
}
}
我在与使JSON对象的问题,我想要做的是连接字符串,而循环,然后这些隐蔽使用jQuery.parseJSON方法的对象......但我没有这样做正确。我甚至尝试在创建temp1时更改引号。
我知道这是复杂的,我可能会问了很多,但任何人都可以看到我在做什么错。我对jQuery.parseJSON并不熟悉,所以任何建议都会很棒,因为我觉得我疯了!
如果我含糊其辞或不解释自己也请说出来......我也把这个小提琴... http://jsfiddle.net/itakesmack/JSRt7/1/
请注意此项工作进展,以便一些项目可能会丢失或者需要开发(或者我可能有其他的错误......但我不希望)。
使用字符串函数永远无法JSON。在适当的对象上使用'JSON.stringify()'。 – ThiefMaster 2012-04-21 10:48:05
这是一个很好的建议...我会给它一个去,谢谢! – 2012-04-21 11:12:44
还记得不要在你的JSON中添加实际的评论,就好像它是JavaScript; JSON不允许他们(除非你把它们作为对象的一部分) – 2012-04-21 11:22:02