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
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
<textarea>
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> element, we again use the
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>
、<select>
或 <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!

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

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

The article discusses the <iframe> tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex

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