Simple and easy to understand jQuery: HTML forms and jQuery
Disable/enable form elements
Using jQuery, you can easily disable a form element by setting the form element's disabled attribute value to disabled. To do this, we simply select an input and use the attr()
method to set the input's disabled attribute to the disabled value.
<!DOCTYPE html> <html lang="en"> <body> <input name="button" type="button" id="button" value="Click me"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <script> (function ($) { $('#button') .attr('disabled', 'disabled'); })(jQuery); </script> </body> </html>
To enable a disabled form element, we simply use removeAttr()
to remove the disabled attribute, or use attr()
to set the disabled attribute value to null.
<!DOCTYPE html> <html lang="en"> <body> <input name="button" type="button" id="button" value="Click me" disabled="disabled"> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <script> (function($){ $('#button').removeAttr('disabled'); // or // $('#button').attr('disabled', ''); })(jQuery); </script> </body> </html>
How to determine whether a form element is disabled or enabled
You can easily select and determine (boolean) whether a form element is disabled or enabled using the jQuery form filter expression :disabled
or :enabled,
. Check the code below for clarification.
<!DOCTYPE html> <html lang="en"> <body> <input name="button" type="button" id="button1"> <input name="button" type="button" id="button2" disabled="disabled"> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <script> (function ($) { // Is it enabled? alert($('#button1').is(':enabled')); // Alerts true // Or, using a filter alert($('#button1:enabled').length); // Alerts "1" // Is it disabled? alert($('#button2').is(':disabled')); // Alerts "true" // Or, using a filter alert($('#button2:disabled').length); // Alerts "1" })(jQuery); </script> </body> </html>
Select/clear a single checkbox or radio button
You can select a radio button input or checkbox by using attr()
to set its checked
attribute to true
.
<!DOCTYPE html> <html lang="en"> <body> <input name="" value="" type="checkbox"> <input name="" value="" type="radio"> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <script> (function($){ // Set all check boxes or radio buttons to selected $('input:checkbox,input:radio').attr('checked', 'checked'); })(jQuery); </script> </body> </html>
To clear a radio button input or checkbox, simply use the removeAttr()
method to remove the checked attribute or set the checked
attribute value to an empty string.
<!DOCTYPE html> <html lang="en"> <body> <input name="" type="checkbox" value="Test1" checked="checked"> <input name="" type="radio" value="Test2" checked="checked"> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <script> (function($){ $('input').removeAttr('checked'); })(jQuery); </script> </body> </html>
Select/clear multiple checkbox or radio button inputs
You can use jQuery's val()
on multiple checkbox inputs or radio button inputs to set the input to checked. This is done by passing the val()
method an array containing a string consistent with the checkbox input or radio button input value attribute.
<!DOCTYPE html> <html lang="en"> <body> <input type="radio" value="radio1"> <input type="radio" value="radio2"> <input type="checkbox" value="checkbox1"> <input type="checkbox" value="checkbox2"> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <script> (function($){ // Check all radio and check box inputs on the page. $('input:radio,input:checkbox').val(['radio1', 'radio2', 'checkbox1', 'checkbox2']); // Use explicit iteration to clear. // $('input:radio,input:checkbox').removeAttr('checked'); // or // $('input:radio,input:checkbox').attr('checked', ''); })(jQuery); </script> </body> </html>
Note: If a checkbox or radio button is selected, using val()
will not clear the input element.
Determine whether a checkbox or radio button is selected or cleared
We can use the :checked
form filter to determine whether a checkbox input or radio button input is checked or cleared. Check the code below to see several uses of the :checked
filter.
<!DOCTYPE html> <html lang="en"> <body> <input checked="checked" type="checkbox"> <input checked="checked" type="radio"> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <script> (function($){ // Alerts "true" alert($('input:checkbox').is(':checked')); // Or, added to wrapper set if checked. Alerts "1" alert($('input:checkbox:checked').length); // Alerts "true" alert($('input:radio').is(':checked')); // Or, added to wrapper set if checked. Alerts "1" alert($('input:radio:checked').length); })(jQuery); </script> </body> </html>
How to determine whether a form element is hidden
You can use the :hidden
form filter to determine whether a form element is hidden. Check the code below to see several uses of the :checked
filter.
<!DOCTYPE html> <html lang="en"> <body> <input type="hidden"> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <script> (function ($) { // Alerts "true" alert($('input').is(':hidden')); // Or, added to wrapper set if hidden. Alerts "1" alert($('input:hidden').length); })(jQuery); </script> </body> </html>
Set/get the value of the input element
val()
The method can be used to set and get the attribute value of the input element (button, checkbox, hidden, image, password, radio, reset, submit, text). Below, I set the value of each input in val()
and then use the val()
method to alert that value.
<!DOCTYPE html> <html lang="en"> <body> <input type="button"> <input type="checkbox"> <input type="hidden"> <input type="image"> <input type="password"> <input type="radio"> <input type="reset"> <input type="submit"> <input type="text"> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <script> (function ($) { $('input:button').val('I am a button'); $('input:checkbox').val('I am a check box'); $('input:hidden').val('I am a hidden input'); $('input:image').val('I am an image'); $('input:password').val('I am a password'); $('input:radio').val('I am a radio'); $('input:reset').val('I am a reset'); $('input:submit').val('I am a submit'); $('input:text').val('I am a text'); // Alerts input's value attribute alert($('input:button').val()); alert($('input:checkbox').val()); alert($('input:hidden').val()); alert($('input:image').val()); alert($('input:password').val()); alert($('input:radio').val()); alert($('input:reset').val()); alert($('input:submit').val()); alert($('input:text').val()); })(jQuery); </script> </body> </html>
Set/get the selected options of the selected element
Using the val()
method, you can set the # by passing a string representing the value assigned to the
<select> element, use the </select>
val() method again to determine which option is selected. The
val() method in this scenario will return the property value of the selected option.
<!DOCTYPE html> <html lang="en"> <body> <select id="s" name="s"> <option value="option1">option one</option> <option value="option2">option two</option> </select> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <script> (function ($) { // Set the selected option in the select element to "option two" $('select').val('option2'); // Alerts "option2" alert($('select').val()); })(jQuery); </script> </body> </html>
Set/get the selected options of multi-select elements
Using the
val() method, we can set the selected value of a multi-select element by passing an array containing the corresponding values to the
val() method.
val() method to retrieve the array of selected options. The array will contain the value attribute of the selected option.
<!DOCTYPE html> <html lang="en"> <body> <select size="4" multiple="multiple"> <option value="option1">option one</option> <option value="option2">option two</option> <option value="option3">option three</option> <option value="option4">option four</option> </select> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <script> (function($){ // Set the value of the selected options $('select').val(['option2', 'option4']); // Get the selected values alert($('select').val().join(', ')); // Alerts, "option2, option4" })(jQuery); </script> </body> </html>
Set/get the text contained in You can set the text node content of a
element by passing a text string to be used as text to the
val()<textarea> method. To get the value of the </textarea>
<textarea> element, we again use the </textarea>
val() method to retrieve the text contained within it.
<!DOCTYPE html> <html lang="en"> <body> <textarea></textarea> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <script> (function ($) { // Set the text contained within $('textarea').val('I am a textarea'); // Alerts "I am a textarea" alert($('textarea').val()); })(jQuery); </script> </body> </html>
Set/get the value attribute of the button element
You can set the value attribute of a button element by passing a text string to the
val() method. To get the value of the button element, use the
val() method again to retrieve the text.
<!DOCTYPE html> <html lang="en"> <body> <button>Button</button> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <script> (function ($) { // Set the value: <button value="I am a Button Element"> $('button').val('I am a Button Element') // Alerts "I am a Button Element" alert($('button').val()); })(jQuery); </script> </body> </html>
Edit Select Element
jQuery makes some common tasks related to editing selected elements trivial. Below are some of these tasks with coding examples.
// Add options to a select element at the end $('select').append('<option value="">option</option>'); // Add options to the start of a select element $('select').prepend('<option value="">option</option>'); // Replace all the options with new options $('select').html('<option value="">option</option><option value="">option</option>'); // Replace items at a certain index using the :eq() selecting filter to // select the element, and then replace it with the .replaceWith() method $('select option:eq(1)').replaceWith('<option value="">option</option>'); // Set the select elements' selected option to index 2 $('select option:eq(2)').attr('selected', 'selected'); // Remove the last option from a select element $('select option:last').remove(); // Select an option from a select element via its // order in the wrapper set using custom filters $('#select option:first'); $('#select option:last'); $('#select option:eq(3)'); $('#select option:gt(5)'); $('#select option:lt(3)'); $('#select option:not(:selected)'); // Get the text of the selected option(s), this will return the text of // all options that are selected when dealing with a multi-select element $('select option:selected').text(); // Get the value attribute value of an option in a select element $('select option:last').val(); // Getting the :last option element // Get the index (0 index) of the selected option. // Note: Does not work with multi-select elements. $('select option').index($('select option:selected')); // Insert an option after a particular position $('select option:eq(1)').after('<option value="">option</option>'); // Insert an option before a particular position $('select option:eq(3)').before('<option value="">option</option>');
Select form elements by type
You can select form elements by type, for example
$('Input: Checkbox'). jQuery provides the following form type filters for selecting form elements by type.
:text
:密码
:radio
:checkbox
:提交
:image
:重置
:file
:button
选择所有表单元素
您可以使用 :input
表单过滤器选择所有表单元素。此过滤器不仅会选择输入元素,还会选择任何 <textarea></textarea>
、<select></select>
或 <button></button>
元素。在下面的编码示例中,请注意使用 :input
过滤器时包装器集的长度。
<!DOCTYPE html> <html lang="en"> <body> <input type="button" value="Input Button"> <input type="checkbox"> <input type="file"> <input type="hidden"> <input type="image"> <input type="password"> <input type="radio"> <input type="reset"> <input type="submit"> <input type="text"> <select> <option>Option</option> </select> <textarea></textarea> <button>Button</button> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <script> (function($){ // Alerts "13" form elements alert($(':input').length); })(jQuery); </script> </body> </html>
The above is the detailed content of Simple and easy to understand jQuery: HTML forms and jQuery. For more information, please follow other related articles on the PHP Chinese website!

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

The role of HTML is to define the structure and content of a web page through tags and attributes. 1. HTML organizes content through tags such as , making it easy to read and understand. 2. Use semantic tags such as, etc. to enhance accessibility and SEO. 3. Optimizing HTML code can improve web page loading speed and user experience.

HTMLisaspecifictypeofcodefocusedonstructuringwebcontent,while"code"broadlyincludeslanguageslikeJavaScriptandPythonforfunctionality.1)HTMLdefineswebpagestructureusingtags.2)"Code"encompassesawiderrangeoflanguagesforlogicandinteract

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!