如何使用下拉URL选择的内容填充文本区域?

时间:2018-07-19 21:54:06

标签: javascript php html ajax

好的,我想这全都归结为:我该如何改善这段代码...

    <br><br>
    <select id="newText">
        <option value="url1">tool_ver1</option>
        <option value="url2">tool_ver2</option>
        <option value="url2" selected >tool_ver3</option>
    </select>
    <br><br>
    <button type="submit">Retrieve New Release App Options</button>
    <br><br>

要填充以下文本区域...

    <div class="textInput spacer">
        <h2>New Release App Options</h2>
        <textarea id="newText"></textarea>
    </div>

最后,textarea需要填充为newText选择的所有url的内容。我无法使用“提交”按钮来完成我希望/期望的事情。

4 个答案:

答案 0 :(得分:1)

使用jQuery,选择元素并使用val方法。在纯js中,查询元素并使用value成员。

//jQuery
$(document).ready(function(){
  $("textarea#mytextarea").val("My value");
});

//OR pure js
document.getElementById('mytextarea').value = 'My value';
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea id='mytextarea'></textarea>

答案 1 :(得分:1)

这是使用不带jquery的纯js的简单答案。 您可以根据需要对其进行修改,以进行“提交”操作,但我可以进行“更改”。

const select = document.getElementById('select')
const newText = document.getElementById('newText')

select.addEventListener("change", e => {
  console.log(e.target.value)
  newText.value = e.target.value
})
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  <select id="select">
    <option value="url1">tool_ver1</option>
    <option value="url2">tool_ver2</option>
    <option value="url3" selected >tool_ver3</option>
  </select>
  <button type="submit">Retrieve New Release App Options</button>
<div class="textInput spacer">
  <h2>New Release App Options</h2>
  <textarea id="newText"></textarea>
</div>
  
  
</body>
</html>

答案 2 :(得分:0)

我认为您正在挂断电话,因为您的