简体   繁体   English

使用javascript或jquery将值从下拉菜单传递到textarea

[英]pass value from drop down menu to textarea with javascript or jquery

Using Jquery or javascript. 使用Jquery或javascript。 What is the best way to send the value of an option from a drop down menu to the textarea listed below. 将选项的值从下拉菜单发送到下面列出的textarea的最佳方法是什么。 So if the user selects one of the names in the drop down it will sends its value to the textarea field. 因此,如果用户选择下拉列表中的一个名称,它将将其值发送到textarea字段。 the select menu is in its own iframe (iframe name is boxmain) and the text area is in its own iframe (iframe name is boxform). 选择菜单位于其自己的iframe中(iframe名称为boxmain),文本区域位于其自己的iframe中(iframe名称为boxform)。

<select id="usernames" name="usernames">
    <option value=""></option>
    <option value="name001">JeffCool</option>
    <option value="name002">IAMsam</option>
</select>

<textarea name="name" cols="15" rows="1" class="class"></textarea>

The simplest way is to use a .change() event handler on your <select> , the simplest form of this: 最简单的方法是在<select>上使用.change()事件处理程序,这是最简单的形式:

 $("#usernames").change(function() {
   $("textarea[name=pst]").val($(this).val());
 });

What this does is what the <select> changes value, it takes the .val() (the value of the <option> just chosen) and sets that value on the <textarea> using the same method. 这样做的是<select>更改值,它采用.val() (刚刚选择的<option>value )并使用相同的方法在<textarea>设置该值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM