我正在使用以下脚本根据另一个字段中的选择填充两个文本字段。我让它在jsFiddle中工作,但它在实际网站上不起作用。有问题的页面是:
http://www.ortorderdesk.com/product/prepare-for-your-closing-copy/
脚本是:
// JavaScript Document
// Pre populated array of data
var myData1 = new Array();
myData1['DFW Locations...'] = 'Blah';
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['DFW Locations...'] = 'Blah';
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'] = 'Addison, TX 75001';
myData2['Legacy'] = 'Addison, TX 75001';
myData2['Mansfield'] = 'Addison, TX 75001';
myData2['McDermott'] = 'Addison, TX 75001';
myData2['Preston Center'] = 'Addison, TX 75001';
document.getElementsByClassName('cart')[1].input_1.onchange = updateText;
function updateText() {
var obj_sel = document.getElementsByClassName('cart')[1].input_1;
document.getElementsByClassName('cart')[1].input_19.value = myData1[obj_sel.value];
var obj_sel = document.getElementsByClassName('cart')[1].input_1;
document.getElementsByClassName('cart')[1].input_21.value = myData2[obj_sel.value];
}
添加了[1]
,因为另一个项目<li>
与“cart”类一起存在。
当我调试并从下拉列表中选择时,我收到错误“未捕获的TypeError:无法读取未定义的属性值”,我理解这意味着我的某个引用已关闭。但是,当我查看它时,我的所有类,ID和名称引用似乎都与页面上的代码匹配。
有人可以查看页面: http://www.ortorderdesk.com/product/prepare-for-your-closing-copy/
答案 0 :(得分:0)
将某些附加 li
与class="cart"
添加到DOM(总共有两个),所以你需要document.getElementsByClassName('cart')[2]
无论DOM中class="cart"
有多少元素,此代码都应该有效:
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];
element.input_21.value = myData2[obj_sel.value];
}