Home  >  Article  >  Web Front-end  >  Jquery knowledge point three jquery form object operation_jquery

Jquery knowledge point three jquery form object operation_jquery

WBOY
WBOYOriginal
2016-05-16 18:12:02932browse

In Jquery, if these three functions have parameters, they are assignment operations. If there are no parameters, they are value acquisition operations. Among them, val() is a very important method, and its related form objects such as: input system labels, select, textarea etc. are all tag elements used to interact with the server, so you need to understand this val();
For assignment operations of radio, checkbox, and select:

Copy code The code is as follows:

$("input[name=a]").val(["Entertainment 1"]) ;
$("input[type=checkbox]").val(["Basketball", "Game"]);
$("select").val(["Basketball", "Game"] ; [], if there are multiple values, separate them with commas;
Assign a value to the select and obtain the select through the label selector.

For the value operations of radio, checkbox, and select:



Copy code
The code is as follows:var selectvalue = "";
$("select :selected").each(function() {
selectvalue = $(this).val();
});
alert("checkvalue:" checkvalue "radiovalue:" s "selectvalue:" selectvalue);



Code analysis:

declares an s The variable is used to receive the selected value of the radio with name=a. It can also be written as var s=$(":radio:checked").val(). :radio can be understood as the input element of type=radio, which is an attribute. A simplified way of writing a selector;
Get the selected value of the checkbox. Because the checkbox is a multi-select box, each selected item needs to be processed with each. It can also be written according to the attribute selector: $(" input[type=checkbox]:checked");
For select, when the attribute multiple ="multiple", multiple selections can be made. Here, each is also used for traversal processing;

Summary:
For input-based tag elements, we can use the attribute selector to obtain: $("input[type=checkbox]"), or in a concise way: $(":checkbox"), similar ones:
: radio, :submit, :image, :reset, :button, :file, :hidden, :password, :text;
$(":input")Select all