How to clear text inside text field when radio but

2019-07-12 15:47发布

Currently I have this radio buttons

  1. Eletronics
  2. Computers
  3. Others

What I am trying to do is, if radio button Others is selected, I would like to display an input text field and let the user type.

What I would like to do is, when I select Others and type something inside the input field, then when I choose back to Eletronics or Computers, I would like to clear the text that I wrote inside input field.

Please kindly provide me with the solution of JavaScript or jQuery.

Here is my code sample. Please kindly check it: http://jsfiddle.net/dnGKM/

7条回答
地球回转人心会变
2楼-- · 2019-07-12 16:43

Try with this Solution:

$(function() {
      $('input[type="radio"]').click(function() {
       if($(this).attr('id') == 'others') {
           $('#others-text').show();
       } else {
           $('#others-text').hide();
           $('#others-text').val('');
       }
   });
})

Here there is a JSFiddle link:

http://jsfiddle.net/dnGKM/4/

查看更多
登录 后发表回答