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

时间:2016-06-25 13:27:15

标签: 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>
你能告诉我我做错了吗?

3 个答案:

答案 0 :(得分:4)

更改

$("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>

答案 1 :(得分:2)

试试这个,

$(document).ready(function(){
    $("#submethodbudget").click(function(){
        $("#smethod").show(200);
                $("#smethod2").hide(200);
    });
    $("#submethodclicks").click(function(){
        $("#smethod2").show(200);
        $("#smethod").hide(200);
    });
});

你甚至可以使用jquery-css方法尝试这个。

答案 2 :(得分:1)

您的代码是正确的!我有一些聪明的代码,试一试。

 $("input[name=submethod]").click(function() {        
        $('div').hide("300");
        $(this).next("div").show("300");        
   }); 
相关问题