简体   繁体   中英

Output HTML on select change in jQuery

Give me examples jquery select value echo content as:

在此处输入图片说明

  • if select item 1 echo div content 1
  • if select item 2 echo div content 2

etc.

My code :

<select>
  <option value="Item 2">Item 2</option>
  <option value="Item 1">Iteam 1</option>
  <!-- ....more -->
  <option value="Item more">Item more</option>
</select>

<div class="value1">Content for select item 1</div>
<div class="value2">Content for select item 2</div>
.....
<div class="valuem">Content for select item more</div>

Thanks

html

 <select>
    <option value="Item 1">Item 1</option>
    <option value="Item 2">Item 2</option>
    <option value="Item 3">Item 3</option>
    <option value="Item 4">Item 4</option>
    <option value="Item 5">Item 5</option>
    <option value="Item 6">Item 6</option>
</select>
<span class="response"></span>

jquery

$(document).ready(function(){
    $("select").change(function(){
        $(".response").text("This is content "+$(this).val());
    });
});

demo jsfiddle

<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
    $('select').change(function() {
       $("#result").text("This is content "+$(this).val());
    });
});
</script>

jQuery:

$("select").on("change", function() {
    $("#contentItem").text("The new content is " + $(this).val());
});

HTML:

<select>   
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>    
</select>    
<div id="contentItem">
</div>   

DEMO

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