简体   繁体   English

多个下拉菜单可动态更改文本表单字段

[英]Multiple Drop-down Menus Dynamically Changing Text Form Fields

With the help of some fine people here at Stack Overflow, I was able to come up with some script that allowed me to dynamically change some text form fields based on the user's selection from a drop-down menu. 在Stack Overflow的一些优秀人员的帮助下,我能够提出一些脚本,该脚本使我能够根据用户从下拉菜单中的选择动态更改某些文本表单字段。 Now I would like to implement two drop-down menus affecting different text form fields. 现在,我想实现两个影响不同文本表单字段的下拉菜单。 I can have them each working on their own, but when I go to combine them, only one works. 我可以让它们各自独立工作,但是当我将它们组合在一起时,只有一部作品。

input_1 and input_39 are my drop-down menus. input_1input_39是我的下拉菜单。

There might be times when one or the other is not present on the page. 有时,页面上可能没有一个。

You can view it on this page: http://www.ortorderdesk.com/product/2-sided-bc/ 您可以在此页面上查看它: http : //www.ortorderdesk.com/product/2-side-bc/

var element = document.querySelector('form.cart');

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];
}

var element = document.querySelector('form.cart');
element.input_39.onchange = updateText;

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

    if (element.input_33)
        element.input_33.value = myData5[obj_sel_value];
    if (element.input_40)
        element.input_40.value = myData6[obj_sel_value];
}

You're overwriting the first instance by re-creating the same variable. 您将通过重新创建相同的变量来覆盖第一个实例。 Try simply removing the second instance of var element = document.querySelector('form.cart'); 尝试简单地删除var element = document.querySelector('form.cart');的第二个实例var element = document.querySelector('form.cart'); .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM