admin管理员组文章数量:1278653
Currently I have this radio buttons
- Eletronics
- Computers
- 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: /
Currently I have this radio buttons
- Eletronics
- Computers
- 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/dnGKM/
Share Improve this question edited Apr 24, 2012 at 12:51 Teun Zengerink 4,3935 gold badges32 silver badges32 bronze badges asked Apr 24, 2012 at 9:42 spotlightsnapspotlightsnap 1,1137 gold badges21 silver badges26 bronze badges7 Answers
Reset to default 3UPDATE
$('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
You can use this:
document.getElementById("others_text").value='';
to clear the input field.
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("puters").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.
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/dnGKM/4/
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="puters">Computers</label>
<input type="radio" id="puters" 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('');
}
});
});
===============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();
}
});
$("#<%=text1.ClientID%>").val('');
本文标签: javascriptHow to clear text inside text field when radio button is selectStack Overflow
版权声明:本文标题:javascript - How to clear text inside text field when radio button is select - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741242258a2364208.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论