我正在尝试在 Javascript 中创建一个二维关联数组(第一个索引数字,第二个索引关联)。我正在设计的页面将有多个字段,以及一个允许用户选择不同“更新日期”的下拉菜单,并且这些字段将重新填充该更新的值。我正在使用 php 生成 javascript 代码:
这是 php 代码的样子(我对其进行了格式化以使其更易于阅读):
<script type='text/javascript'>
<?php
$i = 0;
echo "var updates = new Array();";
foreach ($risk_data['updates'] as $update) {
echo "updates[" . $i . "] = new Array();";
foreach ($update as $key => $value) {
echo "updates[" . $i . "]['" . $key . "'] = '" . $value . "';";
}
$i++;
}
?>
function update_fields() {
var update_index = document.getElementById('select_update').selectedIndex;
alert(update_index);
document.getElementById('impact').innerHTML = updates[update_index]['impact'];
document.getElementById('probability').innerHTML = updates[update_index]['probability'] + '%';
document.getElementById('impact_effect').innerHTML = updates[update_index]['impact_effect'];
document.getElementById('cost_impact').innerHTML = '$' + updates[update_index]['cost_impact'];
document.getElementById('overall_impact').innerHTML = updates[update_index]['overall_impact'];
document.getElementById('expected_cost').innerHTML = '$' + updates[update_index]['expected_cost'];
document.getElementById('impact_discussion').innerHTML = updates[update_index]['impact_discussion'];
document.getElementById('priority_effect').innerHTML = updates[update_index]['priority_effect'];
document.getElementById('priority_monetary').innerHTML = updates[update_index]['priority_monetary'];
}
</script>
供您参考,这是页面加载时的样子:
<script type="text/javascript">
var updates = new Array();
updates[0] = new Array();
updates[0]['date_of_update'] = '2013-08-26';
updates[0]['impact'] = 'delay in schedule';
updates[0]['probability'] = '18';
updates[0]['impact_effect'] = '79';
updates[0]['cost_impact'] = '21000.00';
updates[0]['overall_impact'] = '14';
updates[0]['expected_cost'] = '3780.00';
updates[0]['impact_discussion'] = 'Critical path items past schedule, invoke contract penalties. You can change a very limited number of settings related to formatting. open up netbeans IDE Go to tools->options click on Editor button on top left of the options dialog box click on lot';
updates[0]['priority_effect'] = '1';
updates[0]['priority_monetary'] = '1';
function update_fields() {
var update_index = document.getElementById('select_update').selectedIndex;
alert(update_index);
document.getElementById('impact').innerHTML = updates[update_index]['impact'];
document.getElementById('probability').innerHTML = updates[update_index]['probability'] + '%';
document.getElementById('impact_effect').innerHTML = updates[update_index]['impact_effect'];
document.getElementById('cost_impact').innerHTML = '$' + updates[update_index]['cost_impact'];
document.getElementById('overall_impact').innerHTML = updates[update_index]['overall_impact'];
document.getElementById('expected_cost').innerHTML = '$' + updates[update_index]['expected_cost'];
document.getElementById('impact_discussion').innerHTML = updates[update_index]['impact_discussion'];
document.getElementById('priority_effect').innerHTML = updates[update_index]['priority_effect'];
document.getElementById('priority_monetary').innerHTML = updates[update_index]['priority_monetary'];
document.getElementById('risk_statement').innerHTML = "If " + <?php
echo $risk_data['event'] . " by " .
$risk_data['date_of_concern'];
?> + " then " updates[update_index]['impact'] + ".";
}
</script>
但由于某种原因,脚本根本没有运行。它应该由元素的“onchange”事件调用,但没有任何反应。我不确定错误是否与我声明数组的方式有关。如果有人有任何提示,我将不胜感激!