2014-02-21 108 views
0

嗨我有這個JS函數的問題。我試圖將一個String變量和html元素傳遞給一個JS函數,這樣我可以改變其他元素的值。JS參數和關聯數組

JS

function resetSequence(biz_hrs,form){ 
var employees= [ 
{ 
"esc_hrs":"B" 
"esc_resp":5004 
"maximun":4 
"minimun":1 
} 
{ 
"esc_hrs":"B" 
"esc_resp":5005 
"maximun":4 
"minimun":1 
} 
{ 
"esc_hrs":"B" 
"esc_resp":5006 
"maximun":2 
"minimun":1 
} 
{ 
"esc_hrs":"B" 
"esc_resp":5007 
"maximun":0 
"minimun":0 
} 
{ 
"esc_hrs":"B" 
"esc_resp":5054 
"maximun":3 
"minimun":1 
}, 
]; 
for(var i = 0; i< employees.length; i++){ 
if(biz_hrs === employees[i].esc_hrs && form.value === employees[i].esc_resp){ 
var f = document.escform; 
f.sequence.max=employees[i].maximun; 
f.sequence.val=employees[i].minimun; 
} 
} 

HTML

<select id="p_escalation_resp_i" name="p_escalation_resp_i" class="validate[required]" onChange="resetSequence('B',this)"> 
<option value="5004">Database</option> 
<option value="5005" selected >Application</option> 
<option value="5006">Network Connectivity</option> 
<option value="5007">Operating System Hardware</option> 
<option value="5054">TEST</option> 
     </select> 

<input type="number" name="p_escalation_sequence_i" id="sequence" value="1" step="1" min="0" max="" selected /> 

我的問題是,如果字符參數B是好的,如果用於cicle也不錯

+1

你爲什麼逃避了這麼多,只是通過''B''在正常! – tymeJV

+0

我在其他博客的回答中看到,你必須這樣做? –

+0

只有當你的引號要取消另一個,在你的情況下,他們不會。 – tymeJV

回答

0

你的問題不是很清楚,但我可以推斷你想改變序列輸入的最大值和最小值,對吧?

條件:

if(biz_hrs === employees[i].esc_hrs && form.value === employees[i].esc_resp){ 

始終返回false,因爲form.value是字符串和employees[i].esc_resp是整數。

然後嘗試引用在數組定義的employees[i].esc_resp值:

var employees= [ 
{ 
"esc_hrs":"B", 
"esc_resp":"5004", 
"maximun":4, 
"minimun":1 
}, 
{ 
"esc_hrs":"B" 
"esc_resp":"5005" 
"maximun":4 
"minimun":1 
}, 
{ 
"esc_hrs":"B", 
"esc_resp":"5006", 
"maximun":2, 
"minimun":1 
}, 
{ 
"esc_hrs":"B", 
"esc_resp":"5007", 
"maximun":0, 
"minimun":0 
}, 
{ 
"esc_hrs":"B", 
"esc_resp":"5054", 
"maximun":3, 
"minimun":1 
} 
]; 

注重逗號跡象(,)和引號(」「)。

完整的代碼爲我的作品:

<html> 
<head></head> 

<body> 
    <select id="p_escalation_resp_i" name="p_escalation_resp_i" class="validate[required]" onChange="resetSequence('B',this);"> 
    <option value="5004">Database</option> 
    <option value="5005" selected >Application</option> 
    <option value="5006">Network Connectivity</option> 
    <option value="5007">Operating System Hardware</option> 
    <option value="5054">TEST</option> 
</select> 

<input type="number" name="p_escalation_sequence_i" id="sequence" value="1" step="1" min="0" max="" selected /> 

<script> 
function resetSequence(biz_hrs,form){ 

var employees= [ 
{ 
"esc_hrs":"B", 
"esc_resp":"5004", 
"maximun":4, 
"minimun":1 
}, 
{ 
"esc_hrs":"B", 
"esc_resp":"5005", 
"maximun":4, 
"minimun":1 
}, 
{ 
"esc_hrs":"B", 
"esc_resp":"5006", 
"maximun":2, 
"minimun":1 
}, 
{ 
"esc_hrs":"B", 
"esc_resp":"5007", 
"maximun":0, 
"minimun":0 
}, 
{ 
"esc_hrs":"B", 
"esc_resp":"5054", 
"maximun":3, 
"minimun":1 
} 
]; 

for(var i = 0; i< employees.length; i++){ 
if(biz_hrs === employees[i].esc_hrs && form.value === employees[i].esc_resp){ 
    var f = document.getElementById("sequence"); 
    f.max=employees[i].maximun; 
    f.val=employees[i].minimun; 
    break; 
} 
} 

} 
</script> 
</body> 
</html>