简体   繁体   English

JavaScript连接多个下拉选择

[英]JavaScript connecting several drop down selections

Im newbie in JavaScript, so I was wondering if I have several drop down selections, how can I display a message of all selections that are made? 我是JavaScript的新手,所以我想知道是否有几个下拉选择,如何显示所有选择的消息?

Eg: I have a drop down selection of colors, second with sizes, third with amount. 例如:我有一个下拉选项,第二种是尺寸,第三种是数量。 So, I want to display a message of all the things chosen, such as: you choose "yellow", "size M", "2 pieces". 因此,我想显示一条消息,其中包含所有选定的内容,例如:您选择“黄色”,“尺寸M”,“ 2件”。 Like specification of buyers choices. 像买家选择规格。

I tried like this: 我这样尝试过:

<script> 
    function dialog() {
        var color= document.getElementById ("color").value;
        var size = document.getElementById ("size").value;
        var amount= document.getElementById ("amount").value;

        document.getElementById("demo").innerHTML="You choose" +color+ "size" +size "in an amount of" +amount+;
    }
</script> 

But it works only if I have one drop down, not when I have more... Thanks for help. 但是,只有当我下拉菜单时它才起作用,而当我下拉菜单时则不起作用...谢谢您的帮助。

I would suggest adding a default value to the dropdowns, something like: 我建议为下拉菜单添加一个默认值,例如:

<select>
  <option value="unselected">Select a Size</option>
</select>

Then inside your dialog function, check the values you retreived from the dropdowns to see if they match something other than 'unselected': 然后在对话框函数中,检查您从下拉列表中获取的值,看是否与“ unselected”以外的值匹配:

if (color != 'unselected' && size != 'unselected' && amount != 'unselected') {
  document.getElementById("demo").innerHTML="You choose" +color+ "size" +size+ "in an amount of" +amount;
}

This way you can also isolate which field hasn't been selected yet. 这样,您还可以隔离尚未选择的字段。 Example: 例:

if (color == 'unselected') {
  alert('You need to select a color');
}

Here is a working snippet example of the combined suggestions and edits from the Rob Brander and Scott Kaye . 这是来自Rob BranderScott Kaye的综合建议和编辑的有效片段示例。 The gist is to have isolated code for user interface and another set of code for validation that simply checks if the answer is not the default answer. 要点是拥有用于用户界面的隔离代码和用于验证的另一组代码,这些代码仅检查答案是否不是默认答案。

Update 更新资料

Per Rob's recommendation, I added an else condition so that the dialog() will clear out the text if the input of all 3 drop lists are invalid. 根据Rob的建议,我添加了else条件,以便在所有3个下拉列表的输入均无效时, dialog()将清除文本。

 function dialog() { var color = " " + document.getElementById("color").value + ", "; var size = " " + document.getElementById("size").value + ", "; var amount = " " + document.getElementById("amount").value + "."; var defaultVal = "unselected"; var colorDefault = document.getElementById("color").value; var sizeDefault = document.getElementById("size").value; var amountDefault = document.getElementById("amount").value; if (colorDefault != defaultVal && sizeDefault != defaultVal && amountDefault != defaultVal) { document.getElementById("demo").innerHTML = "You choose color" + color + "size" + size + "in an amount of" + amount; } else { document.getElementById("demo").innerHTML = ""; } } document.getElementById("color").addEventListener("click", dialog); document.getElementById("size").addEventListener("click", dialog); document.getElementById("amount").addEventListener("click", dialog); document.getElementById("color").addEventListener("mousedown", dialog); document.getElementById("size").addEventListener("mousedown", dialog); document.getElementById("amount").addEventListener("mousedown", dialog); document.getElementById("color").addEventListener("mouseup", dialog); document.getElementById("size").addEventListener("mouseup", dialog); document.getElementById("amount").addEventListener("mouseup", dialog); 
 div { width: 80%; border: solid 1px #000; background-color: #ACE; height: 125px; margin-top: 10px; padding: 3px; font-family: arial; } 
 <select id='color'> <option value="unselected">Color...</option> <option value="red">Red</option> <option value="blue">Blue</option> <option value="orange">Orange</option> </select> <select id='size'> <option value="unselected">Size...</option> <option value="7">7</option> <option value="8">8</option> <option value="32">32</option> </select> <select id='amount'> <option value="unselected">Amount...</option> <option value="10">10</option> <option value="20">20</option> <option value="30">30</option> </select> <div id='demo'></div> 

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

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