简体   繁体   English

更新下拉选项值和脚本 onchange 而不是 onclick

[英]Update dropdown option value and script onchange rather than onclick

I am simply trying to do away with the 'Result' button in the following script.我只是想取消以下脚本中的“结果”按钮。 I am happy for the first dropdown options to be selected on page load, and the script to already have run based on this output:我很高兴在页面加载时选择第一个下拉选项,并且脚本已经基于此输出运行:

 var dd1 = document.getElementById("dropdown"); var dd2 = document.getElementById("dropdown2"); var res = document.getElementById("result"); var output = document.getElementById("output"); function getSpecificText(v1, v2) { var con = v1.toString() + v2.toString(); var res = ""; switch (con) { case "500012": res = text1 "; break; case " 500024 ": res = " text2 "; break; case " 500036 ": res = " text3 "; break; case " 500048 ": res = " text4 "; break; case " 500060 ": res = " text1 "; break; case " 1000012 ": res = " text2 "; break; case " 1000024 ": res = " text3 "; break; case " 1000036 ": res = " text4 "; break; case " 1000048 ": res = " text1 "; break; case " 1000060 ": res = " text2 "; break; case " 1500012 ": res = " text3 "; break; case " 1500024 ": res = " text4 "; break; case " 1500036 ": res = " text1 "; break; case " 1500048 ": res = " text2 "; break; case " 1500060 ": res = " text3 "; break; case " 2000012 ": res = " text3 "; break; case " 2000024 ": res = " text4 "; break; case " 2000036 ": res = " text1 "; break; case " 2000048 ": res = " text2 "; break; case " 2000060 ": res = " text3 "; break; case " 2500012 ": res = " text3 "; break; case " 2500024 ": res = " text4 "; break; case " 2500036 ": res = " text1 "; break; case " 2500048 ": res = " text2 "; break; case " 2500060 ": res = " text3 "; break; // ... } return res } function getSelected() { var v1 = dd1.options[dd1.selectedIndex].value; var v2 = dd2.options[dd2.selectedIndex].value; output.innerHTML = getSpecificText(v1, v2); } res.addEventListener(" click ", getSelected);
 <p> Value: <select name="dropdown" id="dropdown"> <option value="5000">5000</option> <option value="10000">10000</option> <option value="15000">15000</option> <option value="20000">20000</option> <option value="25000">25000</option> </select> Months: <select name="dropdown2" id="dropdown2"> <option value="12">12</option> <option value="24">24</option> <option value="36">36</option> <option value="48">48</option> <option value="60">60</option> </select> Months <button id="result">Result</button> <div id="output"></div> </p>

I think you just want to use the onchange event on the drop down list elements.我认为您只想在下拉列表元素上使用onchange事件。 Something like this:像这样的东西:

dd1.addEventListener("change", getSelected);
dd2.addEventListener("change", getSelected);

Here is a working example这是一个工作示例

If you want to show the output when the page is first loaded just call the getSelected function directly in your javascript:如果您想在首次加载页面时显示输出,只需直接在您的 javascript 中调用getSelected函数:

getSelected();

As this function relies on DOM elements, you will need to ensure they are loaded.由于此函数依赖于 DOM 元素,因此您需要确保它们已加载。 This can be done either by putting the script tag at the end of the document, or by using a window.onload event, like so:这可以通过将脚本标签放在文档末尾或使用window.onload事件来完成,如下所示:

window.onload = function(){
    getSelected();
};

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

相关问题 Javascript 文本链接 onClick 以选择下拉选项值 (onChange?) - Javascript text link onClick to select dropdown option value (onChange?) 怎么做<script> update onchange rather than just onload - How to make <script> update onchange rather than just onload 将下拉菜单行为更改为悬停而不是onclick - Changing dropdown behavior to hover rather than onclick 如何使用选项文本而不是在Angular JS中使用选项值在下拉列表中选择值 - How to select a value in dropdown using option text rather than using option value in Angular JS 使用选项输出而不是选项值 - use option output rather than option value .onchange或.onclick并将复选框值添加到JS脚本 - .onchange or .onclick and checkbox value to JS script React-Select:如何在选定值 onChange 上更新选定选项下拉列表的默认值? - React-Select: How do I update the selected option dropdown's defaultValue on selected value onChange? 在下拉菜单中选择选项时,如何使用onChange更改状态,但使用选项标签中的值以外的其他值 - When selecting an option in a dropdown, how do I change the state with onChange but with a value other than the one inside the option tags 根据另一个下拉 onchange 函数设置下拉选项值 - set dropdown option value based another dropdown onchange function 禁用动态下拉选项值 onchange another dynamic dropdown - Disable dynamic dropdown option value onchange another dynamic dropdown
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM