我有一个带有表单的 php 站点,我在该表单上通过 php 输出预选值。在提交表单时,我想检查哪些值已更改,然后通过 javascript 提交这些值。
这些是我从 php.ini 传递过来的预选值。保持关联数组结构很重要。
var pbData = jQuery.parseJSON("{
"GameMode":"DEATHMATCH",
"Current Map":"VEGAS JUNKYARD",
"Current Missions":["VEGAS JUNKYARD","VILLA","PRESIDIO","KILL HOUSE","MURDERTOWN","CQB TRAINING","STREETS","THREE KINGDOMS CASINO","IMPORT\/EXPORT;"],
"RoundDuration":"3 minutes"}");
我在代码中标记了错误。
<script>
function displayVars(){
var form = document.getElementById('settings');
var elems = form.elements;
var txt = "";
for (var index = 0; index < elems.length; index++){
var selIndex = elems[index].selectedIndex;
if (typeof selIndex !== "undefined"){
//the Index Name in the json-object and the name of the form-field are the same
var idxName = elems[index].name;
//HERE is the problem. I want to access the subobject via a variablename, so i can iterate through it, but that doesnt work.
console.log ("pbData default = "+pbData.idxName); //always undefined
if (elems[index].value !== pbData.idx_name){
//building a POST-Url
txt = txt + elems[index].name + "=" + elems[index].options[selIndex].value+"&";
}
}
}
console.log (txt);
return false;
}
</script>
我知道我可以用不同的方式来做这件事,也可以使用 jQuery。就我而言,因为无论如何我都将预选值作为 php 变量,所以我认为这样更容易。我真的很想知道如何通过包含对象名称的变量来遍历子对象。