2017-01-21 17 views
0

我有这个数组对象:getelementsbyid

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; 
} 

时选择的变化,我想获得场命名为“成本”,而不是下面的记录

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

你的意思是'echo $ yourVariable [0] - > cost;'?! – Rizier123

+0

@ Rizier123是的,我的意思是这个,但在JavaScript –

+0

'arrayName [0] .cost'或'arrayName [0] ['cost']'? – mtizziani

回答

0

使用document.getAttribute()的ID:如果您在下面的语句获取成本的价值越来越ID properly.The的价值。

function run() { 
var dropdown = document.querySelector('#corsi'); 
document.getElementById("tot").value = dropdown.getAttribute("cost"); 
} 
+0

抱歉,但它不工作,你可以更详细? –

+0

什么是'$ corsi'?在下拉列表中 –

+0

是数组对象...在$ corsi中有属性成本 –