0

我希望在选择按钮时显示/隐藏单选按钮内容,但我需要一个默认按钮(比如第一个按钮)作为默认按钮(并显示内容),然后从那里显示隐藏内容。

目前,默认情况下它们都是隐藏的,在类上显示:无。这是HTML:

<div style="float:left; width:300px;">
<div style="float:left;">
<form name="form1" id="my_form" method="post" action="">
  <div>
    <label>
    <input type="radio" name="group1" value="opt1" checked="checked" />
    opt1</label>
  </div>
  <div>
    <label>
    <input type="radio" name="group1" value="opt2" />
    opt2</label>
  </div>
  <div>
    <label>
    <input type="radio" name="group1" value="opt3" />
    opt3</label>
  </div>
  <input type="submit" value="Submit" />
</form>
</div>
<div style="float:right; width:200px;">
<div id="opt1" class="desc">
  <form>
    First name:
    <input type="text" name="FirstName" value="Mickey" />
    <br />
    Last name:
    <input type="text" name="LastName" value="Mouse" />
    <br />
  </form>
</div>
<div id="opt2" class="desc">
  <form>
    <input type="checkbox" name="vehicle" value="Bike" />
    I have a bike<br />
    <input type="checkbox" name="vehicle" value="Car" />
    I have a car
  </form>
</div>
<div id="opt3" class="desc">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus convallis commodo euismod. Morbi vitae libero erat, quis ultrices purus.</div>
</div>
</div>

这是jQuery:

$(document).ready(function(){ 
$("input[name$=group1]").click(function() {
    var test = $(this).val();
    $(".desc").hide();
    $("#"+test).show();
}); 
});
4

1 回答 1

1

在 dom 上触发更改,为您检查的元素做好准备

$(document).ready(function() {

    $("input[name$=group1]").change(function() {
        var test = $(this).val();
        $(".desc").hide();
        $("#" + test).show();
    });
    $("input[name$=group1]:checked").change(); // <-- here
});​

http://jsfiddle.net/8hU6K/2/

于 2012-08-29T16:17:48.370 回答