Home >Web Front-end >JS Tutorial >How to implement radio's first click to select and second click to cancel

How to implement radio's first click to select and second click to cancel

小云云
小云云Original
2018-01-12 09:30:223244browse

This article mainly introduces the relevant knowledge of jQuery to implement the radio first click to select and the second click to cancel function, which has a good reference value. Let's take a look with the editor below, I hope it can help everyone.

Due to the needs of the project, the radio is required to be in a canceled state after being clicked twice. It is inconvenient to change it to a checkbox. It can be achieved with a positive method.

// jquery
  $('input:radio').click(function(){
    //alert(this.checked);
    //
    var $radio = $(this);
    // if this was previously checked
    if ($radio.data('waschecked') == true){
      $radio.prop('checked', false);
      $radio.data('waschecked', false);
    } else {
      $radio.prop('checked', true);
      $radio.data('waschecked', true);
    }
  });

Related recommendations:

jQuery gets the radio button selected value and removes all radio selected status examples to share

jQuery implements RadioButton to do the required verification function method sharing

How to use radio in html

The above is the detailed content of How to implement radio's first click to select and second click to cancel. 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