这个二维关联数组有什么问题



我正在尝试用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"事件调用,但什么也没发生。我不确定错误是否可能是在我声明数组的方式上。如果有人有什么建议,我真的很感激!

JavaScript数组[]不是关联的,与PHP数组完全不同。。。

在PHP中,你可以做:

$array = array(
    "foo" => "bar",
    "bar" => "foo"
);

和:

$array2 = array(
    "foo",
    "bar"
);

相联的或非相联的。

在JavaScript中,人们称之为关联数组的是Object Literal(数组也是Object,但在这一点上不要让它混淆)。

要模仿与上述相同的行为,您可以执行以下操作:

var myArray = {
    "foo": "bar",
    "bar": "foo",
};

和:

var myArray2 = [
    "foo",
    "bar"
];

使用JS对象文字(myArray),可以使用点表示法定义属性:myArray.foo = "bar";myArray["foo"] = "bar";

但是对于Arrays,您需要使用.push()方法:myArray.push("bar");


虽然我不建议使用PHP来创建JavaScript对象,但您需要更改函数以使用对象文字而不是数组。

一些不错的阅读:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects

最新更新