.val() | JQuery API Documentation

Example 1

Get the single value from a single select and an array of values from a multiple select and display their values.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 <!doctype html><html lang="en"><head> <meta charset="utf-8"> <title>val demo</title> <style> p { color: red; margin: 4px; } b { color: blue; } </style> <script src="https://code.jquery.com/jquery-3.7.1.js"></script></head><body> <p></p> <select id="single"> <option>Single</option> <option>Single2</option></select> <select id="multiple" multiple="multiple"> <option selected="selected">Multiple</option> <option>Multiple2</option> <option selected="selected">Multiple3</option></select> <script>function displayVals() { var singleValues = $( "#single" ).val(); var multipleValues = $( "#multiple" ).val() || []; // When using jQuery 3: // var multipleValues = $( "#multiple" ).val(); $( "p" ).html( "<b>Single:</b> " + singleValues + " <b>Multiple:</b> " + multipleValues.join( ", " ) );} $( "select" ).on( "change", displayVals );displayVals();</script> </body></html>

Demo:

Từ khóa » Câu Lệnh Val Trong Jquery