HTML 复选框分组

发布于 2024-09-02 07:28:41 字数 58 浏览 5 评论 0原文

通常 HTML 复选框根据名称进行分组。是否可以对名称以外的复选框进行分组?

谢谢你!

Normally HTML check boxes are grouped based on name. Is it possible to group check box other than name?

Thank You!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岁月静好 2024-09-09 07:28:41

不是 HTML 格式。但是您可以在服务器端编写一些代码逻辑,根据某些条件对它们进行分组,例如参数名称甚至参数值中的公共前缀或后缀。

然而,这只是笨拙的。只需通过名称对它们进行分组即可受益于 HTML 所提供的好处。如果您遇到问题,那么需要在其他地方修复它,可能使用元素 ID(表示文档树中的唯一性)或元素样式类(表示文档树中的常见含义)。

Not in HTML. But you can write some code logic in the server side which groups them based on some condition, e.g. a common prefix or suffix in the parameter name or even parameter value.

However, that's only clumsy. Just profit the benefit HTML you gave with the ability to group them by just the name. If you have a problem with that, then it needs to be fixed somewhere else, maybe with the element ID (to denote uniqueness in the document tree) or the element style class (to denote a common meaning in the document tree).

随遇而安 2024-09-09 07:28:41

如果您使用的是 ASP.NET,则可以使用 AjaxToolkit 中的 MutuallyExclusiveCheckBoxExtender 将复选框分组在一起。
它将它们分组在一起,以便一次只能选中组中的 1 个复选框,就像单选按钮组一样。

http://www.asp.net/ajax/tutorials/创建互斥复选框-cs

If you are using ASP.NET, you can use the MutuallyExclusiveCheckBoxExtender in the AjaxToolkit to group checkboxes together.
It groups them together so that only 1 checkbox in the group can be checked at a time, much like radiobutton groups.

http://www.asp.net/ajax/tutorials/creating-mutually-exclusive-checkboxes-cs

夜无邪 2024-09-09 07:28:41

您可以尝试按照您的方式对添加的项目进行排序。
在以下 ASP.Net 复选框列表示例中,我使用比较器对项目进行降序排序(按您的方式更改):

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim allChk As New ArrayList(Me.CheckBoxList1.Items)
        allChk.Sort(New SortComparer(False))
        Me.CheckBoxList1.Items.Clear()
        For Each item As ListItem In allChk
            Me.CheckBoxList1.Items.Add(item)
        Next
    End Sub

    Public Class SortComparer
        Implements IComparer
        Private ascending As Boolean

        Public Sub New(ByVal asc As Boolean)
            Me.ascending = asc
        End Sub

        Public Function [Compare](ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
            Return x.ToString.CompareTo(y.ToString) * DirectCast(IIf(Me.ascending, 1, -1), Int32)
        End Function
    End Class

You can try sort the added items in your way.
In following example for an ASP.Net Checkboxlist i used a comparer to sort the items descending(change it in your way):

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim allChk As New ArrayList(Me.CheckBoxList1.Items)
        allChk.Sort(New SortComparer(False))
        Me.CheckBoxList1.Items.Clear()
        For Each item As ListItem In allChk
            Me.CheckBoxList1.Items.Add(item)
        Next
    End Sub

    Public Class SortComparer
        Implements IComparer
        Private ascending As Boolean

        Public Sub New(ByVal asc As Boolean)
            Me.ascending = asc
        End Sub

        Public Function [Compare](ByVal x As Object, ByVal y As Object) As Integer Implements IComparer.Compare
            Return x.ToString.CompareTo(y.ToString) * DirectCast(IIf(Me.ascending, 1, -1), Int32)
        End Function
    End Class
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文