Javascript HTML - 将 SELECT 标签内容放入 INPUT type = "text"
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2539520/
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
HTML - Put SELECT tag content into INPUT type = "text"
提问by mouthpiec
I have a form in a webpage where I would like to put the selected item in a drop down list into a testbox. The code I have till now is the following:
我在网页中有一个表单,我想将下拉列表中的所选项目放入测试框中。到目前为止,我拥有的代码如下:
<form action = "">
<select name = "Cities">
<option value="----">--Select--</option>
<option value="roma">Roma</option>
<option value="torino">Torino</option>
<option value="milan">Milan</option>
</select>
<br/>
<br/>
<input type="button" value="Test">
<input type="text" name="SelectedCity" value="" />
</form>
I think I need to use javascript .... but any help? :-)
我想我需要使用 javascript .... 但有什么帮助吗?:-)
thanks
谢谢
回答by jholster
You can add JavaScript directly into the button:
您可以将 JavaScript 直接添加到按钮中:
<input type="button" onclick="
var s = this.form.elements['Cities'];
this.form.elements['SelectedCity'].value =
s.options[s.selectedIndex].textContent">
回答by Dekryptid
<script type="text/javascript">
function OnDropDownChange(dropDown) {
var selectedValue = dropDown.options[dropDown.selectedIndex].value;
document.getElementById("txtSelectedCity").value = selectedValue;
}
</script>
<form action = "">
<select name = "Cities" onChange="OnDropDownChange(this);">
<option value="----">--Select--</option>
<option value="roma">Roma</option>
<option value="torino">Torino</option>
<option value="milan">Milan</option>
</select>
<br/>
<br/>
<input type="button" value="Test">
<input type="text" id="txtSelectedCity" name="SelectedCity" value="" />
</form>
回答by Sabbir
In fact you don't need any JS to do that, simply HTML can do it for you as follows:
事实上,你不需要任何 JS 来做到这一点,简单的 HTML 就可以为你做到,如下所示:
<form action="a.php" method="post">
<select name = "Car">
<option value="BMW">BMW</option>
<option value="AUDI">AUDI</option>
</select>
<input type="submit" value="Submit">
</form>

