.val() .val( value ) .val( function(index, value) )
<html> <head> <title>ThaiCreate.Com jQuery Tutorials</title> <style> p { color:red; margin:4px; } b { color:blue; } </style> <script type="text/javascript" src="jquery-1.6.4.js"></script> <script type="text/javascript"> $(document).ready(function(){ function displayVals() { var singleValues = $("#single").val(); var multipleValues = $("#multiple").val() || []; $("p").html("<b>Single:</b> " + singleValues + " <b>Multiple:</b> " + multipleValues.join(", ")); } $("select").change(displayVals); displayVals(); }); </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> </body> </html>
<html> <head> <title>ThaiCreate.Com jQuery Tutorials</title> <style> p { color:blue; margin:8px; } </style> <script type="text/javascript" src="jquery-1.6.4.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("input").keyup(function () { var value = $(this).val(); $("p").text(value); }).keyup(); }); </script> </head> <body> <input type="text" value="some text"/> <p></p> </body> </html>
<html> <head> <title>ThaiCreate.Com jQuery Tutorials</title> <style> button { margin:4px; cursor:pointer; } input { margin:4px; color:blue; } </style> <script type="text/javascript" src="jquery-1.6.4.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("button").click(function () { var text = $(this).text(); $("input").val(text); }); }); </script> </head> <body> <div> <button>Feed</button> <button>the</button> <button>Input</button> </div> <input type="text" value="click a button" /> </body> </html>
<html> <head> <title>ThaiCreate.Com jQuery Tutorials</title> <script type="text/javascript" src="jquery-1.6.4.js"></script> <script type="text/javascript"> $(document).ready(function(){ $('input').bind('blur', function() { $(this).val(function(i, val) { return val.toUpperCase(); }); }); }); </script> </head> <body> <p>Type something and then click or tab out of the input.</p> <input type="text" value="type something" /> </body> </html>
<html> <head> <title>ThaiCreate.Com jQuery Tutorials</title> <style> body { color:blue; } </style> <script type="text/javascript" src="jquery-1.6.4.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("#single").val("Single2"); $("#multiple").val(["Multiple2", "Multiple3"]); $("input").val(["check1","check2", "radio1" ]); }); </script> </head> <body> <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><br/> <input type="checkbox" name="checkboxname" value="check1"/> check1 <input type="checkbox" name="checkboxname" value="check2"/> check2 <input type="radio" name="r" value="radio1"/> radio1 <input type="radio" name="r" value="radio2"/> radio2 </body> </html>
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท