通过 onchange 事件将 SELECT 的值传递给 Javascript 函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3478640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pass the value of a SELECT to a Javascript function via the onchange event?
提问by John M
I have a HTML page that contains a search box containing a number of textboxes.
我有一个 HTML 页面,其中包含一个包含多个文本框的搜索框。
The first part of the search box is a SELECTdropdown list that contains a variety of report types. Each report type requires 1 or more of textboxes to be filled in to filter the query results. My goal is hide the textboxes that are not required by the current report type.
搜索框的第一部分是一个SELECT下拉列表,其中包含各种报告类型。每种报表类型都需要填写 1 个或多个文本框以过滤查询结果。我的目标是隐藏当前报告类型不需要的文本框。
How do I pass the currently selected valuefrom the SELECTto the Javascript function via the onchangeevent?
如何通过onchange事件将当前选定的值从SELECT传递到 Javascript 函数?
<select name="report_type" onchange="hide();">
<option value="full_history">Full History</option>
<option value="partial_history">Partial History</option>
</select>
回答by gabe3886
<select name="report_type" onchange="hide(this.value);">
<option value="full_history">Full History</option>
<option value="partial_history">Partial History</option>
</select>
When doing this the function have whatever value the select currently has.
执行此操作时,该函数具有 select 当前具有的任何值。
回答by learner
function f1()
{
var obj1= document.getElementById("img");
var obj2= document.getElementById("s1");
obj1.src=obj2.value;
}
Select Image:
<select id="s1" onchange="f1()">
<option value="img1.jpg">img1.jpg</option>
<option value="img2.jpg">img2.jpg</option>
<option value="img3.jpg">img3.jpg</option>
<option value="img4.jpg">img4.jpg</option>
</select> <br><br>
<img id='img' src="img1.jpg" height="500" width="500">

