Home Web Front-end JS Tutorial Jquery practical application, determine whether radio, sellct, checkbox is selected and the selected value

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

Dec 21, 2016 pm 03:09 PM

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)!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

Demystifying JavaScript: What It Does and Why It Matters Demystifying JavaScript: What It Does and Why It Matters Apr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

Is JavaScript hard to learn? Is JavaScript hard to learn? Apr 03, 2025 am 12:20 AM

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

How to achieve parallax scrolling and element animation effects, like Shiseido's official website?
or:
How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? How to achieve parallax scrolling and element animation effects, like Shiseido's official website? or: How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? Apr 04, 2025 pm 05:36 PM

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

How to merge array elements with the same ID into one object using JavaScript? How to merge array elements with the same ID into one object using JavaScript? Apr 04, 2025 pm 05:09 PM

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

The Evolution of JavaScript: Current Trends and Future Prospects The Evolution of JavaScript: Current Trends and Future Prospects Apr 10, 2025 am 09:33 AM

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

The difference in console.log output result: Why are the two calls different? The difference in console.log output result: Why are the two calls different? Apr 04, 2025 pm 05:12 PM

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

See all articles