Text boxes, radio buttons, check boxes, related actions
Copy Code code as follows:
var sex=$ ("Input[name= ' Sex ']:checked"). Val (); Gets the value of a set of radio selected items
var item=$ ("#sel option:selected"). Text (); Gets the text of the Select selected item
var option_num=$ (' #sel '). Val (); Get the Select item index
$ ("#sel") [0].selectedindex = 1; The second element of the Select Drop-down box is the currently selected value
$ ("Input[name= ' sex ']"). Get (1). Checked=true; Radio the second element of a radio group is the currently selected value
or select settings by default for the Radio box:
$ ("Input[name= ' sex ']"). each (function () {
if ($ (this). Val () ==s) {
$ (this). attr ("Checked", true);
This.checked=true;
}
});
Jquery Sets Drop-down list (select) Defaults by value
Copy Code code as follows:
<select Name=sel onchange= "Bao (this.options[this.options.selectedindex].value)" >
<option value= "" > Please Select
<option value= "1" >item 1
<option value= "2" >item 2
<option value= "3" >item 3
</select>
<script>
function Bao (s)
{
Txt.value=s;
When you choose, let the first item be selected so that there is a change.
Document.all.sel.options[0].selected=true;
}
</script>
<textarea id=txt></textarea>
Two, jquery get select selected text and value
Copy Code code as follows:
Syntax Explanation:
$ ("#select_id"). Change (function () {//code ...}); Adds an event for a select that fires when one of the items is selected
var checktext=$ ("#select_id"). Find ("option:selected"). Text (); Gets the text selected by select
var checkvalue=$ ("#select_id"). Val (); Gets the value of the Select selection
var checkindex=$ ("#select_id"). Get (0). SelectedIndex; Get the index value selected by select
var maxindex=$ ("#select_id option:last"). attr ("index"); Get the maximum index value for select
jquery Sets the Select selected text and value:
Syntax Explanation:
$ ("#select_id"). Get (0). selectedindex=1; To set the Select index value of 1
$ ("#select_id"). Val (4); Select to set the value of 4 for the Select
$ ("#select_id option[text= ' jQuery ']"). attr ("selected", true); Set the text value of select to the item selected for jquery
jquery Add/Remove option items for select:
Syntax Explanation:
$ ("#select_id"). Append ("<option value= ' value ' >Text</option>"); Append an option to select (dropdown)
$ ("#select_id"). Prepend ("<option value= ' 0 ' > Please select </option>"); Inserts an option for the Select (first position)
$ ("#select_id option:last"). Remove (); Delete index value maximum option (last) in select
$ ("#select_id option[index= ' 0 ']"). Remove (); Delete Option with index value of 0 in select (first)
$ ("#select_id option[value= ' 3 ']"). Remove (); Delete Option value= ' 3 ' in select
$ ("#select_id option[text= ' 4 ']"). Remove (); Delete option text= ' 4 ' in select
Application:
Copy Code code as follows:
<! DOCTYPE html>
<meta charset= "Utf-8" >
<meta http-equiv= "x-ua-compatible" content= "ie=edge,chrome=1" >
<title>jquery common</title>
<meta name= "description" content= "" >
<meta name= "keywords" content= "" >
<link href= "" rel= "stylesheet" >
<script type= "Text/javascript" src= "Jquery-1.8.3.min.js" ></script>
<script type= "Text/javascript" >
$ (document). Ready (function () {
Initialize the Drop-down list--Dynamic add
var item = [' Kindergarten ', ' primary ', ' Junior high ', ' High School ', ' university ', ' postgraduate ', ' PhD ', ' Master '];
var html = "<option value= ' 0 ' > Please select </option>";
for (var i = 0;i < item.length;i++) {
HTML + + "<option value= '" + (i+1) + "' >" +item[i]+ "</option>";
}
$ ("#grade"). Empty (). append (HTML);
$ ("#grade option[value= ' 0 ']"). attr ("Selected", "selected");//Default first item selected
})
Adds an event for a select that fires when one of the items is selected
function Showit () {
var SelectText = $ ("#grade option:selected"). Text ()//Get Select selected text
var SelectText = $ ("#grade"). Find ("option:selected"). Text ();//It works.
var Selectvalue = $ ("#grade"). Val ();//Get selected value
var Selectindex = $ ("#grade"). Get (0). selectedindex//Gets the index value of a SELECT
var text = ' Select: ' +selecttext+ ' \ n ';
Text + = ' value: ' +selectvalue+ ' \ n ';
Text + = ' index value: ' +selectindex;
$ ("#text"). Text (text);
}
</script>
<body>
<div>
<select name= ' grade ' id= ' grade ' onchange= ' showit () ' ></select>
<p><textarea name= ' text ' id= ' text ' row= ' col= ' ></textarea></p>
</div>
</body>