2014-03-27 125 views
1

在本網站上的幾位同事的幫助下,我能夠獲得以下腳本的變體,以根據他們在下拉菜單中進行的選擇填充某些文本字段。基本上,他們從下拉列表中選擇他們的位置,並用他們的地址(input_19)和城市,州和郵政編碼(input_21)填充文本字段。當將其擴展到多個用途時,我意識到有時套件編號需要位於單獨的字段中,因此我添加了兩個額外的變量來分割地址(input_26)和套件編號(input_21)。在沒有input_21的情況下,看起來代碼不成功。只有在字段存在的情況下才有辦法使用腳本部分?只有在物品存在的情況下才定義變量

// JavaScript Document 

// Pre populated array of data 
var myData1 = new Array(); 
myData1['Addison'] = '14295 Midway Road, Suite 100'; 
myData1['Arlington'] = '1241 W. Green Oaks Blvd., Suite 101'; 
myData1['Colleyville'] = '1210 Hall Johnson Road, Suite 150'; 
myData1['Fire Wheel'] = '650 Beebalm Lane, Suite 260'; 
myData1['Flower Mound'] = '3020 Corporate Court, Suite 400'; 
myData1['Fort Worth'] = '4421 Oak Park Lane, Suite 102'; 
myData1['Legacy'] = '7500 Dallas Pkwy., Suite 175'; 
myData1['Mansfield'] = '1405 N. Highway 287, Suite 101'; 
myData1['McDermott'] = '3915 McDermott Road, Suite 2000'; 
myData1['Preston Center'] = '8201 Preston Road, Suite 450'; 
var myData2 = new Array(); 
myData2['Addison'] = 'Addison, TX 75001'; 
myData2['Arlington'] = 'Arlington, TX 76013'; 
myData2['Colleyville'] = 'Colleyville, TX 76034'; 
myData2['Fire Wheel'] = 'Garland, TX 75040'; 
myData2['Flower Mound'] = 'Flower Mound, TX 75028'; 
myData2['Fort Worth'] = 'Fort Worth, TX 76109'; 
myData2['Legacy'] = 'Plano, TX 750241'; 
myData2['Mansfield'] = 'Mansfield, TX 76063'; 
myData2['McDermott'] = 'Plano, TX 75025'; 
myData2['Preston Center'] = 'Dallas, TX 75225'; 
var myData3 = new Array(); 
myData3['Addison'] = 'Suite 100'; 
myData3['Arlington'] = 'Suite 101'; 
myData3['Colleyville'] = 'Suite 150'; 
myData3['Fire Wheel'] = 'Suite 260'; 
myData3['Flower Mound'] = 'Suite 400'; 
myData3['Fort Worth'] = 'Suite 102'; 
myData3['Legacy'] = 'Suite 175'; 
myData3['Mansfield'] = 'Suite 101'; 
myData3['McDermott'] = 'Suite 2000'; 
myData3['Preston Center'] = 'Suite 450'; 
var myData4 = new Array(); 
myData4['Addison'] = '14295 Midway Road'; 
myData4['Arlington'] = '1241 W. Green Oaks Blvd.'; 
myData4['Colleyville'] = '1210 Hall Johnson Road'; 
myData4['Fire Wheel'] = '650 Beebalm Lane'; 
myData4['Flower Mound'] = '3020 Corporate Court'; 
myData4['Fort Worth'] = '4421 Oak Park Lane'; 
myData4['Legacy'] = '7500 Dallas Pkwy.'; 
myData4['Mansfield'] = '1405 N. Highway 287'; 
myData4['McDermott'] = '3915 McDermott Road'; 
myData4['Preston Center'] = '8201 Preston Road'; 




var element = document.querySelector('form.cart'); 
element.input_1.onchange = updateText; 
function updateText() { 
var obj_sel = element.input_1; 
element.input_19.value = myData1[obj_sel.value]; 
var obj_sel = element.input_1; 
element.input_21.value = myData2[obj_sel.value]; 
var obj_sel = element.input_1; 
element.input_26.value = myData3[obj_sel.value]; 
var obj_sel = element.input_1; 
element.input_31.value = myData4[obj_sel.value]; 
} 

謝謝!

回答

0

如果對象不存在,您的腳本會拋出一個錯誤,因爲您正試圖將屬性分配給undefined。由於你沒有catch那個錯誤,腳本就會停止運行。在嘗試設置其值之前,可以使用if語句來確認對象不是undefined。你也不需要你用它每次重複var obj_sel = element.input_1;:這應該解決您的問題

function updateText() { 
    var obj_sel_value = element.input_1.value; 

    if(element.input_19) 
    element.input_19.value = myData1[obj_sel_value]; 
    if(element.input_21) 
    element.input_21.value = myData2[obj_sel_value]; 
    if(element.input_26) 
    element.input_26.value = myData3[obj_sel_value]; 
    if(element.input_31) 
    element.input_31.value = myData4[obj_sel_value]; 
} 

,但也有其他事情可以做,使你的代碼更好,保存自己在未來的時間。例如,不是每個myData對象都有一個變量,您可以使用一組對象。如果您發現自己的變量名稱後綴爲數字,這很好地表明您應該考慮用一個數組替換所有這些變量。

var myData = []; 
myData[0] = {}; 
myData[0]['Addison'] = '14295 Midway Road, Suite 100'; 
... 
myData[0]['Preston Center'] = '8201 Preston Road, Suite 450'; 
myData[1]['Addison'] = 'Addison, TX 75001'; 
... 
myData[1]['Preston Center'] = 'Dallas, TX 75225'; 
myData[2] = {}; 
... 
... 

還要注意,我代替你new Array()電話與{}。這是有道理的,因爲你使用它像一個對象而不是數組。 Javascript數組是一個有序的集合,通過數字鍵進行索引。如果你有字符串鍵,你正在處理一個對象,而不是一個數組。一個對象是(鍵,值)對的無序集合。

+0

@TheUglyBug不客氣。 – Paulpro

相關問題