我有这个对象数组:
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(选择)并创建一个文本框以立即获得选择选择
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;
}
当选择发生变化时,我想获取名为"成本"的字段,而不是记录的 ID
[0] => stdClass Object
(
[cost] => 5 // I want this
)
使用document.getAttribute()
如下:如果您正确获取id的值。以下语句获取成本值。
function run() {
var dropdown = document.querySelector('#corsi');
document.getElementById("tot").value = dropdown.getAttribute("cost");
}