javascript - 单击单选按钮时输入不会消失

标签 javascript html css

我正在尝试编写一个 html css js 代码。在它的一部分中,我有一组单选按钮(包括 2 个元素)。我希望发生这种情况:如果我点击第一个单选按钮,textbox1 出现,如果我点击第二个单选按钮,textbox1 消失,textbox2 出现,反之亦然。

当我点击其中一个时会发生这种情况,但当我点击第二个时它不起作用。

这是我的html代码:

<label>Which one do you want to enter?</label>
<br/>
<label>Budget:</label>
<input name = "submethod" id = "submethodbudget"  type="radio" value = "bud"/>
<div id = "smethod" style="display:none">
   <input type="text" name = "budgetsub">
</div>
<label>Number of Clicks per Month:</label>
<input name = "submethod" id= "submethodclicks" type="radio" value = "clckno"/> 
<div id = "smethod2" style="display:none">
   <input type="text" name = "clicksnosub">
</div>
<br/>

这是我的 js:

<script type="text/javascript">
   $("#submethodbudget").click(function() {
        $("#smethod").show("300");
        $('#smethod').css('display', ($(this).val() === 'bud') ? 'block':'none');
   });
   $("#submethodbudget").click(function() {
        $("#smethod2").hide("300"); 
   });
</script>

<script type="text/javascript">
   $("#submethodclicks").click(function() {
        $("#smethod2").show("300");
        $('#smethod2').css('display', ($(this).val() === 'clckno') ? 'block':'none');
   });
   $("#submethodclicks").click(function() {
        $("smethod").hide("300"); 
   });
</script>

你能告诉我我做错了什么吗?

最佳答案

改变

$("smethod").hide("300"); 

到:

$("#smethod").hide("300");  

我编辑你的代码:

<html>
    <head>
    </head>
    <body>
        <label>Which one do you want to enter?</label>
        <br/>
        <label>Budget:</label><input name = "submethod" id = "submethodbudget"  type="radio" value = "bud"/>
        <div id = "smethod" style="display:none">
            <input type="text" name = "budgetsub">
        </div>
         <label>Number of Clicks per Month:</label><input name = "submethod" id= "submethodclicks" type="radio" value = "clckno"/> 
         <div id = "smethod2" style="display:none">
              <input type="text" name = "clicksnosub">
          </div>
        <br>
        
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
         <script>
             $("#submethodbudget").click(function() {
                 $("#smethod").show("300");
                 $('#smethod').css('display', ($(this).val() === 'bud') ? 'block':'none');
                 $("#smethod2").hide("300"); 
             });

             $("#submethodclicks").click(function() {
                 $("#smethod2").show("300");
                 $('#smethod2').css('display', ($(this).val() === 'clckno') ? 'block':'none');
                 $("#smethod").hide("300"); 
             });
        </script>
    </body>
</html>

关于javascript - 单击单选按钮时输入不会消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38029014/

相关文章:

php - 需要一些关于 jQuery AJAX 请求的帮助,将 PHP 文档加载到 DIV 容器中

javascript - 如何通过 Fable 将 F# 模块的公共(public)函数公开给 Javascript?

javascript - 添加 'active' 类以打开 Bootstrap Accordion 项

html - 如何将 AJAXy 页面更新与后退按钮混合在一起,以便在用户返回时更新仍然存在?

html - 为什么表格宽度和文本大小相对于彼此的比例不同?

html - 平滑无限滚动横幅 [仅 CSS]

javascript - 从购物车中删除在 AngularJS 中不起作用

jquery - 将鼠标悬停在选项卡上时,CSS 会更改 Bootstrap 选项卡控件上选项卡的颜色

javascript - 通过 .ajax() 请求添加动态 HTML 的正确方法

Javascript 困惑