Home > Web Front-end > JS Tutorial > body text

Jquery practical application, determine whether radio, sellct, checkbox is selected and the selected value

黄舟
Release: 2016-12-21 15:09:16
Original
1381 people have browsed it

001 jquery to get the value of the radio radio button

002

003 $("input[name='items']:checked").val();

004 Another: Determine whether the radio is selected and get the selected value

005

006 As shown below:

007 function checkradio(){

008 var item = $(":radio:checked");

009 var len=item. length;

010 if(len> ;0){

011 alert("yes--the selected value is: "+$(":radio:checked").val());

012 }

013 }

014

015

016

017

018 jquery radio value, checkbox value, select value, radio selection, checkbox selection, select selection, and related

019

020 Get the value of a set of radio selected items

021

022 var item = $('input[name=items][checked]').val();

023

024 Get the text of the selected item

025

026 var item = $( "select[name=items] option[selected]").text();

027

028 The second element of the select drop-down box is the currently selected value

029

030 $('#select_id')[ 0].selectedIndex = 1;

031

032 The second element of the radio radio selection group is the currently selected value

033

034 $('input[name=items]').get(1).checked = true;

035

036

037 Get value:

038

039

040

041 Text box, text area: $("#txt").attr("value");

042

043 Multi-select box checkbox: $("#checkbox_id").attr("value");

044

045 Radio group radio: $("input[type=radio][checked]"). val (); Control form elements:

052

053 Text box , text area: $("#txt").attr("value",'');//Clear content

054

055 $("#txt").attr("value",'11') ;//Fill content

056

057

058 Multi-select box checkbox: $("#chk1").attr("checked",'');//Uncheck

059

060 $( "#chk2").attr("checked",true);//Tick

061

062 if($("#chk1").attr('checked')==undefined) //Determine whether it has been Check the box For the currently selected item

067

068 Drop-down box select: $("#sel").attr("value",'-sel3');//Set the item with value=-sel3 as the currently selected item

069

070 $("").appendTo("#sel")//Add drop-down Box option

071

072 $("#sel").empty();//Clear the drop-down box

073

074

075

076                                                                                                                                                    but just starting to come into contact with jquery, many things are unfamiliar to me

078 When using $("#id") to get the input element of the page, I found that $("#id").value cannot get the value

079

080

081

082 Later, finally in great With the help of Baidu, I found the cause of the problem:

083

084 $("") is a jquery object, not a dom element

085

086

087

088 Value is an attribute of dom element

089

090

091

092 jquery corresponds to val

093

094

095 val( ): Get the current value of the first matching element.​​

096 ​

097 ​

098 ​

099 ​ val(val): Set the value of each matching element.

100

101

102

103 So, the code should be written like this:

104

105

106 Value: val = $("#id")[0].value;

107 Assignment : $("#id")[0].value = "new value";

108

109 or $("#id").val("new value"); 112

113 Or you can do this: val = $("#id").attr("value");

114

115

116

117 Each is very easy to use in jQuery and is often used instead. javascript for loop

118

119 For example, if there is an each in a function, if a certain condition in each is true, the function returns true or false

120

121 function method(){

122 . . ..

123 $.each(array,function(){

124 if(condition is true){

125 return true;

126 }

127 }) ;

128 ....

129 }

130

131 It turns out something is always wrong.

132

133 After searching for information, I discovered that break and continue cannot be used in each code block. If you want to implement the functions of break and continue, you must use other methods

134 Break----use return false;

135 continue --use return true;

136

137 So when I want to use return true in each to return this function, in fact, I just let each continue to execute.

138 Even each is not interrupted, so function It cannot be returned

139

140 Another: Determine whether the radio is selected and get the selected value

141

142 As shown below:

143 function checkradio(){

144 var item = $(": radio:checked");

145 var len=item.length;

146 if(len>0){

147 alert("yes--The selected value is: "+$(":radio:checked" ).val());

148 }

149 }

The above is the actual application of Jquery, to determine whether radio, sellct, checkbox is selected and the selected value. For more related content, please pay attention to the PHP Chinese website (www. php.cn)!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!