Javascript 选择单选按钮时如何清除文本字段内的文本
目前我有这个单选按钮Javascript 选择单选按钮时如何清除文本字段内的文本,javascript,jquery,forms,input,radio-button,Javascript,Jquery,Forms,Input,Radio Button,目前我有这个单选按钮 电子 计算机 其他 我想做的是,如果选择了单选按钮Others,我想显示一个输入文本字段,让用户输入 我想做的是,当我选择Others并在输入字段中键入内容时,然后当我选择back toEletronics或Computers时,我想清除我在输入字段中写入的文本 请提供JavaScript或jQuery的解决方案 这是我的代码示例。请检查:您可以使用此: document.getElementById("others_text").value=''; 要清除输入字段,请在
Others
,我想显示一个输入文本字段,让用户输入
我想做的是,当我选择Others
并在输入字段中键入内容时,然后当我选择back toEletronics
或Computers
时,我想清除我在输入字段中写入的文本
请提供JavaScript或jQuery的解决方案
这是我的代码示例。请检查:您可以使用此:
document.getElementById("others_text").value='';
要清除输入字段,请在代码中添加以下行代码:
document.getElementById("others_text").value = '';
现在它看起来像:
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);
这将对您有所帮助。您可以在问题中添加
jQuery
标记,这样就可以使用jQuery:)
CSS:
HTML:
$(“#”)val(“”);
更新
$('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('');
}
});
尝试使用此解决方案:
$(function() {
$('input[type="radio"]').click(function() {
if($(this).attr('id') == 'others') {
$('#others-text').show();
} else {
$('#others-text').hide();
$('#others-text').val('');
}
});
})
这里有一个JSFIDLE链接:
======================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
啊,对不起,是我的错,我不知道那是被禁止的。刚刚编辑了我的答案,我用了一种更常见的方式:)当我通过POST提交结果时,其他人的结果文本框总是空的:(…知道为什么吗?如果我删除js代码,这是($('#其他人文本')。val('');),数据已正确提交,不再是空白数据。解决方案有何解决方法?文本框的名称为
others text
,因此您必须使用$\u GET['others-text']
(PHP)或request.getParameter(“others text”)
(JAVA)问题是,当我用POST提交表单时,我只得到了空白值。我选择“其他”,但是在文本字段中有输入文本。(:还有其他方法来清除文本吗?问题是,当我用POST提交表单时,我只得到空白值。我选择“其他”。但是文本字段中有输入文本。:(.还有其他方法清除文本吗?我已经更新了JSFIDLE。请查看此链接是否有帮助u:当我通过POST提交结果时,其他人的结果文本框始终为空:(…知道为什么吗?如果我删除js代码find('input:text')。val(“”),数据已正确提交,不再是空白数据。有什么解决方法吗?
$("#<%=text1.ClientID%>").val('');
$('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('');
}
});
$(function() {
$('input[type="radio"]').click(function() {
if($(this).attr('id') == 'others') {
$('#others-text').show();
} else {
$('#others-text').hide();
$('#others-text').val('');
}
});
})
<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
$('input[name=rdo]').change(function() {
var a = $(this).val();
if (a != 3) {
$('#textboxid').hide();
}else{
$('#textboxid').val('');
$('#textboxid').show();
}
});