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

时间:2011-12-23 00:29:37

标签: jquery html

所以我在这里问的是,当我点击一个单选按钮时,我能够从我选择的每个单选按钮中看到正确的内容并且正常工作。当我选择另一个单选按钮时,我遇到的问题是背景颜色没有清除,当我选择另一个单选按钮时,如果我也选择另一个单选按钮,我的隐藏输入字段将不会清除。我希望我写的不是混乱。

<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> 

2 个答案:

答案 0 :(得分:0)

背景颜色不会重置,因为您没有重置它。

只需添加此项即可将其重置为原来的状态:

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

答案 1 :(得分:0)

这可能是你想要的东西 - 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");
    }
});