jQuery: brought val into line with documentation

Plus added val test suite
This commit is contained in:
johnnyreilly
2013-12-29 21:42:55 +00:00
parent 3f51b9c7b2
commit 609712265e
2 changed files with 70 additions and 3 deletions
+50
View File
@@ -2309,6 +2309,56 @@ function test_prop() {
var title: string = $('option:selected', this).prop('title');
}
function test_val() {
// Get the value from a dropdown select
$("select.foo option:selected").val();
// Get the value from a dropdown select even easier
$("select.foo").val();
// Get the value from a checked checkbox
$("input:checkbox:checked").val();
// Get the value from a set of radio buttons
$("input:radio[name=bar]:checked").val();
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();
$("input")
.keyup(function () {
var value = $(this).val();
$("p").text(value);
})
.keyup();
$("input:text.items").val(function (index, value) {
return value + " " + this.className;
});
$("button").click(function () {
var text = $(this).text();
$("input").val(text);
});
$("input").on("blur", function () {
$(this).val(function (i, val) {
return val.toUpperCase();
});
});
$("#single").val("Single2");
$("#multiple").val(["Multiple2", "Multiple3"]);
$("input").val(["check1", "check2", "radio1"]);
}
function test_selector() {
var $main = $('#main');
+20 -3
View File
@@ -877,11 +877,28 @@ interface JQuery {
*/
toggleClass(func: (index: number, className: string, swtch: boolean) => string, swtch?: boolean): JQuery;
/**
* Get the current value of the first element in the set of matched elements.
*/
val(): any;
val(value: string[]): JQuery;
/**
* Set the value of each element in the set of matched elements.
*
* @param value A string of text or an array of strings corresponding to the value of each matched element to set as selected/checked.
*/
val(value: string): JQuery;
val(value: number): JQuery;
val(func: (index: any, value: any) => any): JQuery;
/**
* Set the value of each element in the set of matched elements.
*
* @param value A string of text or an array of strings corresponding to the value of each matched element to set as selected/checked.
*/
val(value: string[]): JQuery;
/**
* Set the value of each element in the set of matched elements.
*
* @param func A function returning the value to set. this is the current element. Receives the index position of the element in the set and the old value as arguments.
*/
val(func: (index: number, value: any) => any): JQuery;
/**
* Get the value of style properties for the first element in the set of matched elements.