javascript - 基于单选按钮选择使用 jQuery 隐藏/显示文本框

标签 javascript jquery html onclick radio-button

我需要在我的表单上单击某个单选按钮时显示某些文本框,否则文本框应保持隐藏状态。这是我所拥有的示例:

HTML:

Radio buttons:
<p>Show textboxes<input type="radio" name="radio1" value="Show" onClick="getResults()">Do nothing<input type="radio" name="radio1" value="Nothing"></p>
Textboxes:
<div class="text"><p>Textbox #1 <input type="text" name="text1" id="text1" maxlength="30"></p></div>
<div class="text"><p>Textbox #2 <input type="text" name="text2" id="text2" maxlength="30"></p></div>

jQuery:

$(document).ready(function() {
  $(".text").hide()
    function getResults() {
        $(".text").show()
    };
});

Fiddle

当我取出 .show() 代码时,文本框保持隐藏状态。我需要在选择带有 onClick 事件的单选按钮时显示它们。任何帮助将不胜感激。

最佳答案

JSFIDDLE

Javascript

$(document).ready(function () {
    $(".text").hide();
    $("#r1").click(function () {
        $(".text").show();
    });
    $("#r2").click(function () {
        $(".text").hide();
    });
});

HTML

<p>Show textboxes
    <input type="radio" name="radio1" id="r1" value="Show">Do nothing
    <input type="radio" name="radio1" id="r2" value="Nothing">
</p>Wonderful textboxes:
<div class="text">
    <p>Textbox #1
        <input type="text" name="text1" id="text1" maxlength="30">
    </p>
</div>
<div class="text">
    <p>Textbox #2
        <input type="text" name="text2" id="text2" maxlength="30">
    </p>
</div>

关于javascript - 基于单选按钮选择使用 jQuery 隐藏/显示文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22790767/

相关文章:

html - CSS - 样式似乎妨碍了 Firefox 中的 URL 显示

javascript - 在调用回调之前如何等待异步创建的对象完全可用?

javascript - IE 中的 getElementById.contentDocument 错误

php - Plupload Onload 显示上传文件列表

javascript - 在模型声明中将计算值添加到可观察数组

html - 如何通过完全填充屏幕大小将两个 html 按钮水平排列在一条宽度相等的单行中

javascript - 在 HTML 表中显示对象 ng-repeat - Angular

javascript - 使用 angular/node/javascript 将 Twitter Feed 作为 JSON (tweeter scraper)

javascript - 使用 atan2() 向鼠标方向旋转一个 div

html - 垂直居中单选按钮及其标签