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 theattr()
method to set the input's disabled attribute to the disabled value.
To enable a disabled form element, we simply useremoveAttr()
to remove the disabled attribute, or useattr()
to set the disabled attribute value to null.
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.
You can select a radio button input or checkbox by usingattr()
to set itschecked
attribute totrue
.
To clear a radio button input or checkbox, simply use theremoveAttr()
method to remove the checked attribute or set thechecked
attribute value to an empty string.
You can use jQuery'sval()
on multiple checkbox inputs or radio button inputs to set the input to checked. This is done by passing theval()
method an array containing a string consistent with the checkbox input or radio button input value attribute.
Note:If a checkbox or radio button is selected, usingval()
will not clear the input element.
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.
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.
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 inval()
and then use theval()
method to alert that value.
Using theval()
method, you can set the# by passing a string representing the value assigned to 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.
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.
element by passing a text string to be used as text to the
val()
val()
method to retrieve the text contained within it.
val()method. To get the value of the button element, use the
val()method again to retrieve the text.
// Add options to a select element at the end $('select').append(''); // Add options to the start of a select element $('select').prepend(''); // Replace all the options with new options $('select').html(''); // 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(''); // 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(''); // Insert an option before a particular position $('select option:eq(3)').before('');
$('Input: Checkbox'). jQuery provides the following form type filters for selecting form elements by type.
:text
:密码
:radio
:checkbox
:提交
:image
:重置
:file
:button
您可以使用:input
表单过滤器选择所有表单元素。此过滤器不仅会选择输入元素,还会选择任何、
或
元素。在下面的编码示例中,请注意使用
:input
过滤器时包装器集的长度。
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!