Home  >  Article  >  Web Front-end  >  Simple and easy to understand jQuery: HTML forms and jQuery

Simple and easy to understand jQuery: HTML forms and jQuery

王林
王林Original
2023-08-27 16:17:061215browse

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

To get the value of 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.

To get the selected option in a multi-select element, we again use the

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!

Statement:
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