Javascript 根据已更改的下拉列表填充textarea
我的问题是,我正试图更新一个Javascript 根据已更改的下拉列表填充textarea,javascript,html,drop-down-menu,textarea,Javascript,Html,Drop Down Menu,Textarea,我的问题是,我正试图更新一个textarea的文本,根据该文本,下拉列表已被更改 因此,如果更改下拉列表1,则textarea将更新,但如果更改下拉列表2,则将再次更新textarea。我目前的代码如下: <html> <head> <script type="text/javascript"> function responseText(product) { document.getElementById("resptext").va
textarea
的文本,根据该文本,下拉列表已被更改
因此,如果更改下拉列表1,则textarea
将更新,但如果更改下拉列表2,则将再次更新textarea
。我目前的代码如下:
<html>
<head>
<script type="text/javascript">
function responseText(product) {
document.getElementById("resptext").value = document.getElementById(product).value;
}
function CopyToClipboard() {
let selector = document.querySelector('#resptext')
selector.select()
document.execCommand('copy')
}
</script>
</head>
<body>
<div style="float: left;">Dropdown 1<br />
<select id="thingama" onClick="responseText(thingama); ">
<option></option>
<option value="test 1">1</option>
<option value="test 2">2</option>
</select>
</div>
<br/>
<br/>
<br/>
<div style="float: left;">Dropdown 2<br />
<select id="thingama1" onClick="responseText(thingama1); ">
<option></option>
<option value="test 3">3</option>
<option value="test 4">4</option>
</select>
</div>
<textarea id="resptext" style=" width: 600px; height: 400px;"></textarea>
<button onClick="CopyToClipboard()">Copy</button>
</body>
</html>
函数响应文本(产品){
document.getElementById(“resptext”).value=document.getElementById(product).value;
}
函数CopyToClipboard(){
让选择器=document.querySelector(“#resptext”)
selector.select()
document.execCommand('copy')
}
下拉列表1
1.
2.
下拉列表2
3.
4.
复制
我相信我的问题在于如何传递变量,但我遗漏了一些我认为非常简单的东西。非常感谢您的帮助。您的代码没有什么问题,首先您应该使用onchange,然后您应该使用它而不是名称
函数响应文本(产品){
document.getElementById(“resptext”).value=product.value;
}
函数CopyToClipboard(){
让选择器=document.querySelector(“#resptext”)
selector.select()
document.execCommand('copy')
}
下拉列表1
1.
2.
下拉列表2
3.
4.
复制
出于好奇,为什么这里的onchange比onclick好?onclick不检查更改,它只检查单击的元素,单击可能不会更改元素值ahhh有意义,再次感谢!