简体   繁体   中英

to put the value of two select tag in a <output>, html

I have two select tag, and I want that when a user chose one value in each of the select tag to have an output that shows both the values:

For example if in the first tag someone choose cars and in the seccon golf , I want to have in the output the expression cars,golf ..How can I do this? Please help me

Here is a rough idea , you need to refine it according to your need

<script type="text/javascript">

            $(function(){

            var sb1_val;
            var sb2_val;

             $('#sb1').on('change', function() 
             {
                sb1_val = this.value ;
             });

             $('#sb2').on('change', function()
             {
                sb2_val = this.value ;

                 console.log(sb1_val+','+sb2_val); //this will output the both
                 alert(sb1_val+','+sb2_val); //this will output the both

             });

            });


        </script>


    <select id='sb1'>
        <option value='car1'>car1</option>
        <option value='car2'>car2</option>

    </select>


    <select id='sb2'>
        <option value="golf1">golf1</option>
        <option value='golf2'>golf2</option>

    </select>

DEMO

Here is a working demo in pure JavaScript.

The solution I have proposed is to capture the onchange event and display it in a div tag using innerHTML .

HTML:

<select id="select1">
   <option> cars </option>
   <option> bikes </option>
</select>
<div id="output"></div>

JavaScript:

document.getElementById("select1").onchange=function(){
    var e = document.getElementById("select1");
    selected1  = e.options[e.selectedIndex].text;
    if(selected2!="")
       document.getElementById("output").innerHTML = selected1 + ", " + selected2;

};

Simply use the .change() in jQuery.

$("select").change(function(){
    //output the values
    $("#my_output").val($("#s1").val() + "," + $("#s2").val());
});

demo here .

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