ASP.NET RadioButtonList 本身不支持分組,但您可以通過使用 CSS 和 JavaScript(或 jQuery)來實現分組效果
以下是一個簡單的示例,說明如何使用 CSS 和 JavaScript(或 jQuery)為 RadioButtonList 創建分組:
<asp:RadioButtonList ID="RadioButtonList1" runat="server">
<asp:ListItem Text="Option 1" Value="1" />
<asp:ListItem Text="Option 2" Value="2" />
<asp:ListItem Text="Option 3" Value="3" />
<asp:ListItem Text="Option 4" Value="4" />
</asp:RadioButtonList>
/* 隱藏默認的 RadioButton */
#RadioButtonList1 input[type="radio"] {
display: none;
}
/* 創建分組的容器 */
#RadioButtonList1 label {
display: inline-block;
padding: 10px;
background-color: #f2f2f2;
border: 1px solid #ccc;
cursor: pointer;
}
/* 當 RadioButton 被選中時,改變容器的背景顏色 */
#RadioButtonList1 input[type="radio"]:checked + label {
background-color: #4CAF50;
color: white;
}
// 使用原生 JavaScript
document.querySelectorAll('#RadioButtonList1 label').forEach(function (label) {
label.addEventListener('click', function () {
var radioButton = this.querySelector('input[type="radio"]');
if (radioButton.checked) {
// 當 RadioButton 被選中時,執行操作,例如顯示相關信息
alert('Option ' + radioButton.value + ' is selected.');
}
});
});
// 或者使用 jQuery
$('#RadioButtonList1 label').on('click', function () {
var radioButton = $(this).find('input[type="radio"]');
if (radioButton.is(':checked')) {
// 當 RadioButton 被選中時,執行操作,例如顯示相關信息
alert('Option ' + radioButton.val() + ' is selected.');
}
});
這樣,您就可以使用 CSS 和 JavaScript(或 jQuery)為 ASP.NET RadioButtonList 創建分組效果了。