Javascript 如果使用jquery选中单选按钮,如何显示输入字段
我有一个单选按钮叫other,当选中时,我想显示一个输入字段叫other text。这很简单,但我整个上午都不及格Javascript 如果使用jquery选中单选按钮,如何显示输入字段,javascript,jquery,html,Javascript,Jquery,Html,我有一个单选按钮叫other,当选中时,我想显示一个输入字段叫other text。这很简单,但我整个上午都不及格 <input name="ocalls" type="radio" name="other" id="other-radio" value="Yes"><b>Other</b><br /> <input type="text" name="other-text" id="other-text" style="display:no
<input name="ocalls" type="radio" name="other" id="other-radio" value="Yes"><b>Other</b><br />
<input type="text" name="other-text" id="other-text" style="display:none" />
<pre><input type="radio" name="other" id="other-radio" value="Yes"><b>Other</b></pre>
几乎没有失败 您的代码无法工作,因为您在单选按钮中有两次name属性 使用以下命令:
<input type="radio" name="other" id="other-radio" value="Yes">
删除name=ocalls属性。我建议这样做。尝试使用但发现您使用的是旧版本的jquery 相反,您可以使用复选框: html 这很有效
jQuery("#other-radio").change(function(){...
刚刚更新了您的JSFIDLE: 你的代码工作正常,你只是输入了错误的无线电输入,它有两个名称属性
jQuery(function(){
jQuery("input[name=ocalls]").change(function(){
if($(this).attr("checked"))
{
$("#other-text").show();
}
else
{
$("#other-text").hide();
}
});
});
试试这个。如果你想要切换效果,你可以在复选框中使用这个代码
jQuery("input:checkbox[name='ocalls']").change(function(){
if ($(this).attr("checked") == true) {
jQuery("#other-text").show();
}
else {
jQuery("#other-text").hide();
}
});
jQuery("#other-radio").change(function(){...
<pre><input type="radio" name="other" id="other-radio" value="Yes"><b>Other</b></pre>
<form>
<input name="ocalls" type="radio" name="other" id="other-radio" value="Yes">
<b>Other</b><br />
</input>
<input type="text" name="other-text" id="other-text" style="display: none;" />
</form>
jQuery(function(){
jQuery("#other-radio").change(function(){
if ($(this).val() == "Yes") {
jQuery("#other-text").show()
}
else {
jQuery("#other-text").hide();
}
});
});
jQuery(function(){
jQuery("input[name=ocalls]").change(function(){
if($(this).attr("checked"))
{
$("#other-text").show();
}
else
{
$("#other-text").hide();
}
});
});