在本網站上的幾位同事的幫助下,我能夠獲得以下腳本的變體,以根據他們在下拉菜單中進行的選擇填充某些文本字段。基本上,他們從下拉列表中選擇他們的位置,並用他們的地址(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];
}
謝謝!
@TheUglyBug不客氣。 – Paulpro