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

You add the jQuery tag to your question, so that should do the trick using jQuery :)

CSS:

.hidden {
    display: none;
}

HTML:

<label for="electronics">Electronics</label>
<input type="radio" id="electronics" name="rdbutton" />
<label for="computers">Computers</label>
<input type="radio" id="computers" name="rdbutton" />
<label for="others">Others</label>
<input type="radio" id="others" name="rdbutton" />
<input type="text" id="others-text" name="others-text" class="hidden" />

JS:

$(function() {
    $('input[type="radio"]').click(function() {
        if($(this).attr('id') == 'others') {
            $('#others-text').removeClass('hidden');
        } else {
            $('#others-text').addClass('hidden');
            $('#others-text').val('');
        }
    });
});
查看更多
对你真心纯属浪费
3楼-- · 2019-07-12 16:27

You can use this:

document.getElementById("others_text").value='';

to clear the input field.

查看更多
Emotional °昔
4楼-- · 2019-07-12 16:28

===============HTML

<input type="radio" name="rdo" value="1" checked="checked" />Electronics
<input type="radio" name="rdo" value="2" />Computers
<input type="radio" name="rdo" value="3" />Others

===============jQuery

  $('input[name=rdo]').change(function() {
         var a = $(this).val();
         if (a != 3) {
             $('#textboxid').hide();
         }else{
             $('#textboxid').val('');
             $('#textboxid').show();
         }
   });
查看更多
太酷不给撩
5楼-- · 2019-07-12 16:35
$("#<%=text1.ClientID%>").val('');
查看更多
Animai°情兽
6楼-- · 2019-07-12 16:39

Please add below code of line in your code:

document.getElementById("others_text").value = '';

now its look like:

document.getElementById("others").addEventListener("click", function()
{
    document.getElementById("others_text").value = '';
    document.getElementById("showhide").style.opacity = 1;
}, false);
document.getElementById("computers").addEventListener("click", function()
{
    document.getElementById("showhide").style.opacity = 0;
}, false);
document.getElementById("electronics").addEventListener("click", function()
{
    document.getElementById("showhide").style.opacity = 0;
}, false);​

This would be helpful for you.

查看更多
走好不送
7楼-- · 2019-07-12 16:42

UPDATE

$('input:radio').on('click', function() {
    if($(this).attr('id') == 'others') {
        $('#showhide').css('opacity', 1.0);
   } else {
       $('#showhide').css('opacity', 0).find('input:text#others_text').val('');
    }  
});​

DEMO

查看更多
登录 后发表回答