简体   繁体   English

当我单击另一个单选按钮时,如何清除单选按钮选择中的内容?

[英]How can I clear content from a radio button selection when I click on another radio button?

So what I'm asking here is that when I click on a radio button I'am able to see the correct content from each radio button that I select and that works correctly. 因此,我要问的是,当我单击一个单选按钮时,我能够从我选择的每个单选按钮中看到正确的内容,并且它们可以正常工作。 the problem that I'am having is that the background color is not clearing when I select another radio button, also when I choose the other radio button my hidden input field will not clear if I also select another radio button. 我遇到的问题是,当我选择另一个单选按钮时,背景颜色无法清除,而且当我选择另一个单选按钮时,如果我也选择了另一个单选按钮,则隐藏的输入字段将无法清除。 I hope what I wrote is not confusing. 我希望我写的东西不会混淆。

<script type="text/javascript" charset="utf-8">

  function rb1(){
    $("#img1").attr("src","<%= site_url('images/anonymous.png') %>");
    $("h3#amount").text("$what can $10 do?");
    $("p#amountDescription").text("this is for $10");
    $("#radio1").css("background-color","#73a1d0");
  };

  function rb2(){
    $("#img1").attr("src","<%= site_url('images/bg.png') %>");
    $("h3#amount").text("radio 2");
    $("p#amountDescription").text("this is for $20");
    $("#radio2").css("background-color","#73a1d0");
  };

  function rb3(){
    $("#img1").attr("src","<%= site_url('images/bg.png') %>");
    $("h3#amount").text("radio 3");
    $("p#amountDescription").attr("this is for $20");
    $("#radio3").css("background-color","#73a1d0");
  };

  function rb4(){
    $("#img1").attr("src","<%= site_url('images/bg.png') %>");
    $("h3#amount").text("radio 4");
    $("p#amountDescription").text("this is for $20");
    $("#radio4").css("background-color","#73a1d0");
  };

  function rb5(){
    $("#img1").attr("src","<%= site_url('images/bg.png') %>");
    $("h3#amount").text("radio 5");
    $("p#amountDescription").text("this is for $20");
    $("#radio5").css("background-color","#73a1d0");
  };

  function other(){
    $("#img1").attr("src","<%= site_url('images/bg.png') %>");
    $("#otherInput").show("");
    $("p#amountDescription").text("Other Amount");
    $("#other").css("background-color","#73a1d0");
  };
</script>


  <span id="radio1" class="selection">$10<br/>
    <input onClick="rb1()" type="radio"  name="group_name" CHEKCED value="rb1" />

    <div id="box1">
    <img src="images/" id="img1">
    <h3 id="amount"></h3>
    <p id="otherInput" style="display:none;">$<input class="oInput" type="text" name="" value=""></p>
    <p id="amountDescription"></p>      
    </div>


    </span>
     <span id="radio2" class="selection">$20<br/>
    <input onClick="rb2()"  type="radio" name="group_name" value="rb2"  />
    </span>
     <span id="radio3" class="selection">$30<br/>
    <input onClick="rb3()" type="radio" name="group_name" value="rb3" />
    </span> 
     <span id="radio4" class="selection">$40<br/>
    <input onClick="rb4()" type="radio" name="group_name" value="rb4" />
    </span>
     <span id="radio5" class="selection">$50<br/>
    <input onClick="rb5()" type="radio" name="group_name" value="rb5"/>
    </span>
     <span id="other" class="selection">Other<br/>
    <input onClick="other()" type="radio" name="group_name" value="other" />
    </span> 

The background color isn't resetting because you're not resetting it. 未重置背景颜色,因为您未重置它。

Simply add this to reset it back to whatever it was: 只需添加此命令即可将其重置为原来的状态:

$(".selection").css("background-color","white");

This might be the stuff you want - http://jsfiddle.net/YU7uA/2/ 这可能是您想要的东西-http://jsfiddle.net/YU7uA/2/

<span id="radio1" class="selection">$10<br/>
    <input type="radio"  name="group_name" checked value="rb1" />

    <div id="box1">
    <img src="http://placehold.it/30/30" id="img1">
    <h3 id="amount"></h3>
    <p id="otherInput" style="display:none;">$<input class="oInput" type="text" name="" value=""></p>
    <p id="amountDescription"></p>      
    </div>

</span>

<span id="radio2" class="selection">$20<br/>
    <input type="radio" name="group_name" value="rb2"  />
</span>

<span id="radio3" class="selection">$30<br/>
    <input type="radio" name="group_name" value="rb3" />
</span> 

<span id="radio4" class="selection">$40<br/>
    <input type="radio" name="group_name" id='rb4' value="rb4" />
</span>

<span id="radio5" class="selection">$50<br/>
    <input type="radio" name="group_name" value="rb5"/>
</span>

<span id="other" class="selection">Other<br/>
    <input type="radio" name="group_name" value="other" />
</span> 



$('input:radio').click(function() {
    // clean bg
    $('span').each(function() {
        $(this).css('background-color', '#fff');
    });

    var myValue = ($(this).attr("value")).substr(($(this).attr("value")).length - 1);

    if (myValue === 'r') {
        $('#otherInput').css('display', 'block');
        $("h3#amount").text("");
        $("p#amountDescription").text("");
        $("#other").css("background-color","#73a1d0")
    } else {
        $("#img1").attr("src", "http://placehold.it/30/30");
        $("h3#amount").text("what can $" + myValue + "0 do?");
        $("p#amountDescription").text("this is for $" + myValue + "0");
        $("#radio" + myValue).css("background-color","#73a1d0");
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM