jquery-mobile/experiments/api-viewer/docs/val/index.html
2010-09-16 14:27:10 -04:00

206 lines
No EOL
7.4 KiB
HTML

<!DOCTYPE html>
<html lang='en'><head><meta http-equiv='content-type' content='text/html; charset=UTF-8' /></head><body>
<div class="ui-page">
<div class="ui-header">
<h1>.val()</h1>
</div>
<div class="ui-content ui-body ui-body-c" id="val1">
<h2 class="jq-clearfix roundTop section-title">
<span class="name">.val()</span> <span class="returns">Returns: <a class="return" href="http://docs.jquery.com/Types#String,%20Array">String, Array</a></span>
</h2>
<div class=" entry-details">
<p class="desc"><strong>Description: </strong>Get the current value of the first element in the set of matched elements.</p>
<ul class="signatures"><li class="signature" id="val"><h4 class="name">
<span class="versionAdded">version added: <a href="/category/version/1.0/">1.0</a></span>.val()</h4></li></ul>
<div class="longdesc">
<p>The <code>.val()</code> method is primarily used to get the values of form elements. In the case of <code>&lt;select multiple="multiple"&gt;</code> elements, the <code>.val()</code> method returns an array containing each selected option.</p>
<p>For selects and checkboxes, you can also use the <a href="/selected">:selected</a> and <a href="/checked">:checked</a> selectors to get at values, for example:</p>
<pre>$('select.foo option:selected').val(); // get the value from a dropdown select
$('select.foo').val(); // get the value from a dropdown select even easier
$('input:checkbox:checked').val(); // get the value from a checked checkbox
$('input:radio[name=bar]:checked').val(); // get the value from a set of radio buttons</pre>
</div>
<h3>Examples:</h3>
<div id="entry-examples" class="entry-examples">
<div id="example-0">
<h4>Example: <span class="desc">Get the single value from a single select and an array of values from a multiple select and display their values.</span>
</h4>
<pre><code class="example demo-code">&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;style&gt;
p { color:red; margin:4px; }
b { color:blue; }
&lt;/style&gt;
&lt;script src="http://code.jquery.com/jquery-latest.js"&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;p&gt;&lt;/p&gt;
&lt;select id="single"&gt;
&lt;option&gt;Single&lt;/option&gt;
&lt;option&gt;Single2&lt;/option&gt;
&lt;/select&gt;
&lt;select id="multiple" multiple="multiple"&gt;
&lt;option selected="selected"&gt;Multiple&lt;/option&gt;
&lt;option&gt;Multiple2&lt;/option&gt;
&lt;option selected="selected"&gt;Multiple3&lt;/option&gt;
&lt;/select&gt;
&lt;script&gt;
function displayVals() {
var singleValues = $("#single").val();
var multipleValues = $("#multiple").val() || [];
$("p").html("&lt;b&gt;Single:&lt;/b&gt; " +
singleValues +
" &lt;b&gt;Multiple:&lt;/b&gt; " +
multipleValues.join(", "));
}
$("select").change(displayVals);
displayVals();
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;</code></pre>
<h4>Demo:</h4>
<div class="demo code-demo"></div>
</div>
<div id="example-1">
<h4>Example: <span class="desc">Find the value of an input box.</span>
</h4>
<pre><code class="example demo-code">&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;style&gt;
p { color:blue; margin:8px; }
&lt;/style&gt;
&lt;script src="http://code.jquery.com/jquery-latest.js"&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;input type="text" value="some text"/&gt;
&lt;p&gt;&lt;/p&gt;
&lt;script&gt;
$("input").keyup(function () {
var value = $(this).val();
$("p").text(value);
}).keyup();
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;</code></pre>
<h4>Demo:</h4>
<div class="demo code-demo"></div>
</div>
</div>
</div>
</div>
<div class="ui-content ui-body ui-body-c" id="val2">
<h2 class="jq-clearfix roundTop section-title">
<span class="name">.val( value )</span> <span class="returns">Returns: <a class="return" href="http://docs.jquery.com/Types#jQuery">jQuery</a></span>
</h2>
<div class=" entry-details">
<p class="desc"><strong>Description: </strong>Set the value of each element in the set of matched elements.</p>
<ul class="signatures">
<li class="signature" id="val-value">
<h4 class="name">
<span class="versionAdded">version added: <a href="/category/version/1.0/">1.0</a></span>.val( value )</h4>
<p class="arguement"><strong>value</strong>A string of text or an array of strings to set as the value property of each matched element.</p>
</li>
<li class="signature" id="val-functionindex, value">
<h4 class="name">
<span class="versionAdded">version added: <a href="/category/version/1.4/">1.4</a></span>.val( function(index, value) )</h4>
<p class="arguement"><strong>function(index, value)</strong>A function returning the value to set.</p>
</li>
</ul>
<div class="longdesc">
<p>This method is typically used to set the values of form fields. For <code>&lt;select multiple="multiple"&gt;</code> elements, multiple <code>&lt;option&gt;</code>s can be selected by passing in an array.</p>
<p>The <code>.val()</code> method allows us to set the value by passing in a function. As of jQuery 1.4, the function is passed two arguments, the current element's index and its current value: </p>
<pre>$('input:text.items').val(function(index, value) {
return value + ' ' + this.className;
});
</pre>
<p>This example appends the string " items" to the text inputs' values.</p>
</div>
<h3>Examples:</h3>
<div id="entry-examples" class="entry-examples">
<div id="example-0">
<h4>Example: <span class="desc">Set the value of an input box.</span>
</h4>
<pre><code class="example demo-code">&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;style&gt;
button { margin:4px; cursor:pointer; }
input { margin:4px; color:blue; }
&lt;/style&gt;
&lt;script src="http://code.jquery.com/jquery-latest.js"&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div&gt;
&lt;button&gt;Feed&lt;/button&gt;
&lt;button&gt;the&lt;/button&gt;
&lt;button&gt;Input&lt;/button&gt;
&lt;/div&gt;
&lt;input type="text" value="click a button" /&gt;
&lt;script&gt;
$("button").click(function () {
var text = $(this).text();
$("input").val(text);
});
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;</code></pre>
<h4>Demo:</h4>
<div class="demo code-demo"></div>
</div>
<div id="example-1">
<h4>Example: <span class="desc">Set a single select and a multiple select .</span>
</h4>
<pre><code class="example demo-code">&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;style&gt;
body { color:blue; }
&lt;/style&gt;
&lt;script src="http://code.jquery.com/jquery-latest.js"&gt;&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;select id="single"&gt;
&lt;option&gt;Single&lt;/option&gt;
&lt;option&gt;Single2&lt;/option&gt;
&lt;/select&gt;
&lt;select id="multiple" multiple="multiple"&gt;
&lt;option selected="selected"&gt;Multiple&lt;/option&gt;
&lt;option&gt;Multiple2&lt;/option&gt;
&lt;option selected="selected"&gt;Multiple3&lt;/option&gt;
&lt;/select&gt;&lt;br/&gt;
&lt;input type="checkbox" name="checkboxname" value="check1"/&gt; check1
&lt;input type="checkbox" name="checkboxname" value="check2"/&gt; check2
&lt;input type="radio" name="r" value="radio1"/&gt; radio1
&lt;input type="radio" name="r" value="radio2"/&gt; radio2
&lt;script&gt;
$("#single").val("Single2");
$("#multiple").val(["Multiple2", "Multiple3"]);
$("input").val(["check1","check2", "radio1" ]);
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;</code></pre>
<h4>Demo:</h4>
<div class="demo code-demo"></div>
</div>
</div>
</div>
</div>
</div>
</body></html>