javascript - clear radio buttons when click on text input - Stack Overflow

I have a group of 4 radio buttons followed by a text input, when users click on the text input field I

I have a group of 4 radio buttons followed by a text input, when users click on the text input field I am trying to clear all radio inputs. here is my code so far.

 <input type="radio" name="radio"><label for="radio1">1</label>
 <input type="radio" name="radio"><label for="radio2">2</label>
 <input type="radio" name="radio"><label for="radio3">3</label>
 <input type="radio" name="radio"><label for="radio4">4</label>
 <input type="text" id="textInput">

 <script>
      $('#textinput').click(function () {
          radio.checked = false;
      });
 </script>

I have a group of 4 radio buttons followed by a text input, when users click on the text input field I am trying to clear all radio inputs. here is my code so far.

 <input type="radio" name="radio"><label for="radio1">1</label>
 <input type="radio" name="radio"><label for="radio2">2</label>
 <input type="radio" name="radio"><label for="radio3">3</label>
 <input type="radio" name="radio"><label for="radio4">4</label>
 <input type="text" id="textInput">

 <script>
      $('#textinput').click(function () {
          radio.checked = false;
      });
 </script>
Share Improve this question edited Jul 2, 2022 at 7:59 Jonas 129k102 gold badges327 silver badges405 bronze badges asked Feb 3, 2014 at 20:36 nicknick 1,2563 gold badges21 silver badges41 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 6

You can use .prop() to set checked property of input rabio buttons. Also you have misspelled textInput while event binding

<script>
      $('#textInput').click(function () {
          $('input[name="radio"]').prop("checked", false);
      });
 </script>

DEMO

<script>
      $('#textInput').click(function () {
          $('input[type=radio]').removeAttr("checked");

      });
 </script>

Or you can try attr() method

 $('#textInput').click(function () {
         $('input[name="radio"]').attr('checked',false);

      });

DEMO

I think the best way to modify your script block (without changing your html) is first by ensuring that the code runs on document ready, and also you should probably ensure that the event is focus, not click, in case someone is using a keyboard or alternate navigation:

$(function() {
    $('#textInput').focus(function () {
          $('input[name=radio]').prop("checked", false);
    });
});

Though it's probably more likely that you want to only clear other selections if they actually enter some data in that field, you might want to instead do:

$(function() {    
    $('#textInput').on('input', function () {
        if($(this).val().length > 0) {
            $('input[name=radio]').prop("checked", false);
        }
    });
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744323362a4568529.html

相关推荐

  • javascript - clear radio buttons when click on text input - Stack Overflow

    I have a group of 4 radio buttons followed by a text input, when users click on the text input field I

    8天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信