0

我有 10 个具有唯一名称的单选按钮,这些单选按钮分为两个 div。我的意思是每个 div 有 5 个单选按钮,我想一次只显示一个 div,并选中第一个单选按钮。我想要的是可见的 div 应该默认选中第一个单选按钮。但请注意,所有单选按钮在同一页面上都有唯一的名称。

下面是我的代码供参考。

<style>
#secondopt, #firstopt { display: none;}
</style>

<script>
function checkme(){
if (document.getElementById("opt1").checked == true){
document.getElementById("firstopt").style.display = "block"
document.getElementById("secondopt").style.display = "none";
}
if (document.getElementById("opt2").checked  == true){
document.getElementById("firstopt").style.display = "none"
document.getElementById("secondopt").style.display = "block";
}
}   
</script>

<form name="form1">
<label><input type="radio" name="opt" id="opt1" onclick="checkme()" /> First opt</label>
<label><input type="radio" name="opt" id="opt2" onclick="checkme()" /> Second Opt</label>
<div id="firstopt">
<label><input type="radio" name="items" value="data1" />Item 1</label>
<label><input type="radio" name="items" value="data2" />Item 2</label>
<label><input type="radio" name="items" value="data3" />Item 3</label>
<label><input type="radio" name="items" value="data4"/>Item 4</label>
<label><input type="radio" name="items" value="data5"/>Item 5</label>
</div>
<div id="secondopt">
<label><input type="radio" name="items" value="data6"/>Item 6</label>
<label><input type="radio" name="items" value="data7"/>Item 7</label>
<label><input type="radio" name="items" value="data8"/>Item 8</label>
<label><input type="radio" name="items" value="data9"/>Item 9</label>
<label><input type="radio" name="items" value="data10"/>Item 10</label>
</div>
</form>

如何做到这一点?

4

2 回答 2

1

由于您将单选按钮划分为两个 div,因此您还应该使用单选按钮的组属性/名称。这里给出两个不同的组名称,然后使用单选按钮 id 使用 selected/checked 属性。

于 2013-01-31T12:47:48.883 回答
0
do this



<form name=frmone>
<INPUT TYPE="Radio" Name="payment" Value="CC">Credit Card
<INPUT TYPE="Radio" Name="payment" Value="DC">Debit Card
<INPUT TYPE="Radio" Name="payment" Value="PP">PayPal
</form>
<script>
var len = document.frmOne.payment.length;
for (i = 0; i < len; i++) {

document.frmOne.payment[i].checked ;
break;

}

</script>
于 2013-01-31T12:58:18.693 回答