2017-01-21 20 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中有屬性成本 –