getelementsbyid数组节点对象stdClass对象

时间:2017-01-21 01:49:45

标签: javascript php arrays codeigniter getelementbyid

我有这个对象数组:

Array
(
[0] => stdClass Object
    (
        [id] => 1
        [name] => a
        [cost] => 5
    )

[1] => stdClass Object
    (
        [id] => 2
        [name] => kraftmagar
        [cost] => 10
    )

[2] => stdClass Object
    (
        [id] => 3
        [name] => prepugilistica
        [cost] => 20
    )
)

我已将此数组放入form_dropdown(select)并创建一个文本框以立即获取选择

echo form_dropdown('rif_corso', $corsi, '', 'onChange="run();"     id="corsi"')

$data = array(
      'type'  => 'text',
      'name'  => 'tot',
      'id'    => 'tot',
);

echo form_input($data);

这是javascript函数

function run() {
    document.getElementById("tot").value = document.getElementById("corsi").value;
}

当选择更改时,我想获得名为" cost"不是记录的id

[0] => stdClass Object
(
    [cost] => 5 //  I want this
)

1 个答案:

答案 0 :(得分:0)

使用document.getAttribute()如下:如果你正确获得id的值。以下语句获得cost的值。

function run() {
var dropdown = document.querySelector('#corsi');
document.getElementById("tot").value = dropdown.getAttribute("cost");
}
相关问题