简体   繁体   English

javascript下拉菜单返回值

[英]javascript drop down menus returning a value

been using the site for a while - first time posting. 使用该网站已有一段时间-首次发布。

I have a problem i am trying to solve. 我有一个要解决的问题。 in Javascript (not jQuery or AJAX) i am trying to have 2 drop down menus, and a third field returning an integer based on the two drop-downs. 在Javascript(不是jQuery或AJAX)中,我尝试有2个下拉菜单,并且第三个字段基于两个下拉菜单返回一个整数。 So i have Dropdown menu 1, when the user selects a value from that menu the second dropdown will display the relevant items - this i have running fine using an example i found here: Populate one dropdown based on selection in another 因此,我具有下拉菜单1,当用户从该菜单中选择一个值时,第二个下拉菜单将显示相关项目-使用我在这里找到的示例,我可以很好地运行该菜单:根据另一个选择填充一个下拉菜单

However i now need to be able to pull a value out based on the selection in the second drop-down menu. 但是我现在需要能够根据第二个下拉菜单中的选择提取一个值。 For instance the user selects an "Audi" in the first menu, then "A4" in the second menu and is then shown the price of that specific item. 例如,用户在第一个菜单中选择“ Audi”,然后在第二个菜单中选择“ A4”,然后显示该特定商品的价格。

im at a total loss with this one, been trying to use the index off the second dropdown menu - once a user has selected the third item the index should return "4" i could then set-up another array to switch that "4" to the value i want - but that seems clunky. 我对此完全不知所措,一直在尝试使用第二个下拉菜单中的索引-用户选择了第三项后,索引应返回“ 4”,然后我可以设置另一个数组来切换“ 4”达到我想要的值-但这似乎很笨拙。

In my initial tests for my menu's i was using this array: 在菜单的初始测试中,我使用了以下数组:

    stone_category = 0;
 stone_category["Granite & Marble"]=1;
 stone_category["Sensa Granite"]=2;
 stone_category["Silestone Quartz - Polished Finish"]=3;
 stone_category["Silestone Quartz - Leather & Volcano Finish"]=4;
 stone_category["Eco Recycled Surfaces - Polished Finish"]=5;
 stone_category["Caesarstone Quartz - Polished Finish"]=6;
 stone_category["Unistone Quartz - Polished & Leather Finish"]=7;
 stone_category["Samsung Radianz Quartz - Polished Finish"]=8;
 stone_category["Okite Quartz - Polished Finish"]=9;
 stone_category["Compac Quartz - Polished and Matt Finish"]=10;

And from that i could pull the numeric value to use in a calculation. 从那我可以拉出数值用于计算。 however the way i then had to set up my drop-down menus that style of array doesnt work for the second menu leaving me at the loss i am at now. 但是,我随后必须设置下拉菜单的方式使数组样式不适用于第二个菜单,这让我无所适从。

i hope i have explained clearly enough. 我希望我已经解释清楚了。

You could add an extra attribute to those elements, something like category-id="x" , where x is the value you want to pull from each. 您可以为这些元素添加一个额外的属性,例如category-id="x" ,其中x是您要从每个元素中提取的值。 Then you can grab that value with a selector: 然后,您可以使用选择器获取该值:

value = document.querySelector("#submenu .checked").getAttribute("category-id");

Change #submenu and .checked with whatever classes or selectors you add to the submenu and selected items. 更改#submenu.checked使用添加到该子菜单和所选项目的任何类或选择器。

Thanks for the responses, and i apologise for missing the HTML out - brain frazzle yesterday (been too long since i last coded!). 感谢您的回复,对于我错过了HTML,我们深表歉意。昨天我脑子有点发麻(距离我上一次编码太久了!)。

I the end i managed to fix the problem by creating a switch statement in the final calculation. 最后,我设法通过在最终计算中创建switch语句来解决该问题。 This retrieves the value from the second menu that the user has selected and switches it for the desired value like this: 这将从用户选择的第二个菜单中检索值,并将其切换为所需的值,如下所示:

var a = document.getElementById("ddl2");
var strUser = a.options[a.selectedIndex].value;

//switch the name of the stone for a price //以价格切换石头的名称

if (thicknessDesired == "20") {
switch (strUser) {
case 'Galaxy Grey':
case 'Rosa Porrino':
case 'Rosa Sardo':
        var price = '99.75';
    break;

the switch statement is now growing rapidly hence only posting a snippet. switch语句现在正在快速增长,因此仅发布了一个片段。 this is probably not the most elegant solution but i have found it works just fine, so for now i can plough on to the next issues lol. 这可能不是最优雅的解决方案,但我发现它可以正常工作,所以现在我可以继续研究下一个问题。

Thanks again guys 再次感谢你们

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

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