简体   繁体   中英

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

Using Jquery or javascript. What is the best way to send the value of an option from a drop down menu to the textarea listed below. So if the user selects one of the names in the drop down it will sends its value to the textarea field. 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).

<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:

 $("#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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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