Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/410.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 基于selectbox选择将预定义文本添加到文本区域_Javascript_Html_Select_Textbox - Fatal编程技术网

Javascript 基于selectbox选择将预定义文本添加到文本区域

Javascript 基于selectbox选择将预定义文本添加到文本区域,javascript,html,select,textbox,Javascript,Html,Select,Textbox,我正在visual studio中为我的学校制作一个网站,并以html和javascript编写的联系方式 我在一个选择框中列出了所有教员的姓名,我想这样做,当用户选择教员时,用户将在其中写入消息的文本区域将根据用户的选择自动打开教员的姓名 有人能帮我找到一个javascript库或教程吗?既然你没有提到使用Jquery,我将提供一个简单的javascript答案 $("#selectbox-id").change(function() { var optionSelected = $( "

我正在visual studio中为我的学校制作一个网站,并以html和javascript编写的联系方式

我在一个选择框中列出了所有教员的姓名,我想这样做,当用户选择教员时,用户将在其中写入消息的文本区域将根据用户的选择自动打开教员的姓名


有人能帮我找到一个javascript库或教程吗?

既然你没有提到使用Jquery,我将提供一个简单的javascript答案

$("#selectbox-id").change(function() {
  var optionSelected = $( "#selectbox-id option:selected" ).text();
  $("#textboxarea-id").text(optionSelected);
});
<html>
<head>        
    <script type="text/javascript">
        function SetName() {
            var sel = document.getElementById("Select1");
            var nm = '';
            if (sel.selectedIndex > -1) {
                nm = sel.options[sel.selectedIndex].text;
            }
            document.getElementById("TextArea1").innerHTML = nm;
        }
    </script>
</head>
<body>    
    <select id="Select1" name="D1" onchange="SetName()">
         <option>Mr Green</option>
         <option>Mr White</option>
    </select>
    <textarea id="TextArea1" cols="20" name="S1" rows="2"></textarea>

</body>
</html>

函数SetName(){
var sel=document.getElementById(“Select1”);
var nm=“”;
如果(选择索引>-1){
nm=sel.options[sel.selectedIndex].text;
}
document.getElementById(“TextArea1”).innerHTML=nm;
}
格林先生
怀特先生

到目前为止,您得到了什么/尝试了什么?请提供您的示例代码到目前为止,我的代码仅包括带有值的选择框、文本区域、提交按钮和验证功能。非常好!那么请接受我的答案作为对这个问题的回答。