选择选项使用JavaScript更改Textarea的文本

时间:2018-06-06 13:11:57

标签: javascript jquery html

我需要帮助。我有这样的表格:

<form name="user_cause" method="post">
<div class="form-group">
  <select id="subject" required="required" class="form-control" onchange="showHide()">
    <option value="subject">Subject</option>
    <option value="test_1">Test_1</option>
    <option value="test_2">Test_2</option>
  </select>
</div>

<div class="form-group">
  <textarea id="subject_message" required="required"
            class="form-control" rows="5" disabled>
  </textarea>
</div>

<button type="submit" class="btn btn-primary">Send</button>

我希望我可以根据选择更改已禁用的textarea中的文本。如何用javascript做到这一点?

3 个答案:

答案 0 :(得分:0)

使用jQuery,$("#subject").change(function(e) { $("#subject_message").val($(this).val())});

答案 1 :(得分:0)

jQuery方式:

 $("#subject").change (function() {
      $("#subject_message").text($("#subject option:selected").text();
    });

答案 2 :(得分:0)

var ourArea = document.getElementById('subject_message');
var selectSubject = document.getElementById('subject');
ourArea.textContent = selectSubject.value; // omit this if you don't want initial value to be reflected in the text area
function showHide(){
    ourArea.textContent = selectSubject.value;
}