我有一个select
和一个textarea
如果textarea
元素已更改,我想填写select
这是我的HTML代码(我无法编辑它,所以我需要jQuery):
<div class="event-attributes">
<select name="em_attributes[room]">
<option>room 1</option>
<option>room 2</option>
<option>room 3</option>
</select>
</div>
<div class="event-attributes">
<label for="em_attributes[Room detail]">Room detail</label>
<textarea id="" cols="55" rows="5"></textarea>
</div>
如果我选择房间1,textarea
必须将其值更改为“这是房间描述”等。
我在尝试:
<script type="text/javascript">
var mytextbox = document.getElementById('textareaID');
var mydropdown = document.getElementById('dropdownID');
mydropdown.onchange = function(){
mytextbox.value = mytextbox.value + this.value; //to appened
//mytextbox.innerHTML = this.value;
}
</script>
但我没有选项或textarea的ID
答案 0 :(得分:1)
只需查看以下代码和演示
即可HTML: -
<div class="event-attributes"></div>
<select name="em_attributes[room]" id="slt">
<option>room 1</option>
<option>room 2</option>
<option>room 3</option>
</select>
</div>
<div class="event-attributes">
<label for="em_attributes[Room detail]">Room detail</label>
<textarea id="txt" cols="55" rows="5"></textarea>
</div>
JQUERY: -
$(document).ready(function(){
$("#slt").on('change', function() {
$("#txt").val("You selected : " + $(this).val());
});
});
更新: -
$(document).ready(function(){
$("select[name='em_attributes[room]']").on('change', function() {
$("textarea ").val("selected :"+ $(this).val() + " this is the " + $(this).val() + " description " );
});
});
演示: -
答案 1 :(得分:0)
$(document).ready(function(){
// apply a change event
$('#drodownid').change(function() {
// update input box with the currently selected value
$('#textboxid').val($(this).val());
});
});
答案 2 :(得分:0)
<强> JQuery的强>
$('select').on('change',function (){
$('textarea').val("this is a room description")
});