简体   繁体   中英

How to insert drop-down value into textArea?

Does anyone know how can I insert values from the drop-down list into textarea, when it is being selected? I went searching around the internet and I tried doing it, yet it does not appear at all.

Below is the code snippets of what I have done but I'm stuck where I selected the value from drop-down list, it doesn't appear in my textarea at all. I need help.

Thanks people.

Variable.php

<?php

$variable_arr = array("Mobile", "Name", "Amount", "Due", "Etc");
$variable_str = '<select name="Variable" id="drp_dwn">';            
$variable_str .= '<option selected>&ltSelect Data&gt</option>';

foreach($variable_arr as $variable)
{
    $variable_str .= '<option value="'. $variable .'">&lt'.$variable.'&gt</option>';
}

$variable_str .= '</select>';

?>

change.js

$(document).ready(function() {
    $("#drp_dwn").change(function () {
        var str = "";

        $("selected").each(function () {
            str += $(this).text() + " ";
            });
        $("textArea").text(str);
        }).change();
});

index.php

<textarea rows="4" cols="40" type="text" name="content" id="textArea"></textarea>
<label> <input type="submit" value="Send" name="submit" id="send_box"> </label>

use val() method instead of text()

$("#drp_dwn").change(function () {
        $("#textArea").val(this.value);
 }).change();

DEMO

Try this

$(document).ready(function(){
    //adding event listener
    $('#drp_dwn').on('change', function(){

        //assigning the selected option to a variable
        var str = $(this).val();

        //so sending value to textbox
        $('#textArea').text(str);
    });
});

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