简体   繁体   English

如何更改复选框单击时的 div 边框颜色

[英]How to change a div border color on a checkbox click

I have a custom checkbox that works like a slider.我有一个像滑块一样工作的自定义复选框。 You click the button, it slides across and changes color to green..您单击按钮,它会滑过并将颜色更改为绿色..

Unchecked checkbox未选中的复选框

clicked点击

Checked checkbox勾选复选框

Now, i am trying to figure out, how i could use the same kind of css to change the border color of the surrounding button..现在,我想弄清楚,如何使用相同类型的 css 来更改周围按钮的边框颜色..

I want to change this border, on checkbox checked我想更改此边框,选中复选框

Here is my custom css for the checkbox/slider这是我的复选框/滑块的自定义 css

.checkbox{
                            position:relative;
                            cursor:pointer;
                            appearance:none;
                            width:37px;
                            height:24px;
                            border-radius:20px;
                            border:2px solid #ccc;
                            outline:none;
                            transition:0.3s;
                            border-color:red;
                        }
                        .checkbox::before{
                            content:"";
                            position:absolute;
                            height:15px;
                            width:15px;
                            border-radius:50%;
                            background:red;
                            top:2px;
                            left:0px;
                            transition:0.3s ease-in-out;
                        }
                        .checkbox:checked::before{
                            transform:translateX(18px);
                            background:green;
                        }
                        .checkbox:checked{
                            border-color:green;
                        }

And implementation of the checkbox/slider以及复选框/滑块的实现

    <div class="float-container">
                                                    <div class="float-child">
                                                        <input type="checkbox" class="checkbox" @onchange="eventArgs => { CheckboxClicked(role.Description, eventArgs.Value); }"/>
                                                    </div>
                                                    <div class="float-child">
                                                        <p>Application Role - </p>
                                                        <br />
                                                        <p><b>@role.Description</b></p>
                                                        <br/>
                                                    </div>
                                                </div>

So, i want to implement the same kind of logic to my float logic - css below 

.float-container {
                            border: 3px solid #fff;
                            padding: 20px;
                        }

                        .float-child {
                            width: 100%;
                            padding: 20px;
                            border: 2px solid seagreen;
                            text-align-last: center;
                        }

I am sure this will be easy for somone who is a whizz at CSS.我相信这对于 CSS 高手来说会很容易。 Im still learning..我还在学习..

EDIT****编辑****

So it looks like this can only be done in JS, judging by the comments.. I haven't used js yet, but id be greatful for the help.. So i guess the JS needs to know, when i have selected the checkbox, to change the color of my border in float-child.所以看起来这只能在 JS 中完成,根据评论判断..我还没有使用过 js,但我非常乐意提供帮助..所以我想 JS 需要知道,当我选择了复选框时, 更改浮动子项中边框的颜色。

UPDATE ***更新***

So i figured it out using Javascript - using this script to replace css items when the checkbox was checked - But having other issues, i guess my foreach loop needs unique id's.. not sure how to achieve it, in a dynamic list.. But this code below may help some one, whos not working with dynamic.所以我用 Javascript 想出了它 - 当复选框被选中时,使用这个脚本替换 css 项目 - 但是有其他问题,我猜我的 foreach 循环需要唯一的 id .. 不知道如何在动态列表中实现它.. 但是下面的这段代码可能对一些不使用动态的人有所帮助。

First add id's to the elements you want changing首先将 id 添加到要更改的元素

<div class="float-child" Id="floatdiv">
<input type="checkbox" Id="checkbox" class="checkbox">

And then my javascript to change the css elements..然后我的 javascript 来更改 css 元素..

function BorderColorChangeOnCheckboxClick(){
    $('[id*=checkbox]').click(function () {
        if ($(this).is(":checked")) {
            $("#floatdiv").css("width", "100%");
            $("#floatdiv").css("padding", "20px");
            $("#floatdiv").css("border", "2px solid red");
            $("#floatdiv").css("text-align-last", "center");
        }
        
        else {
            $("#floatdiv").css("width", "100%");
            $("#floatdiv").css("padding", "20px");
            $("#floatdiv").css("border", "2px solid seagreen");
            $("#floatdiv").css("text-align-last", "center");
        }
    });
};

Don't forget, if you are working with Blazor.. To add the javascript file to your host files, and also make the JS interop call within your code.. ie不要忘记,如果您正在使用 Blazor .. 将 javascript 文件添加到您的主机文件,并在您的代码中进行 JS 互操作调用.. 即

protected override async Task OnAfterRenderAsync(bool firstRender)
            {
                await Js.InvokeVoidAsync("search");
                await Js.InvokeVoidAsync("BorderColorChangeOnCheckboxClick");
    
            }

Hope this will help someone in the future..希望这会在将来对某人有所帮助..

So back to my code -所以回到我的代码 -

FULL CODE完整代码

<div hidden="@IsShow" class="text-center-middle">
                        <div class="text-left mb-3">
                            <div class="col-lg-12 control-section">
                                <div class="control_wrapper">
                                    @if (AppRolesDetails != null)
                                    {
                                        foreach (var role in AppRolesDetails)
                                        {
                                            <div class="float-container">
                                                <div class="float-child">
                                                    <input type="checkbox" Id="checkbox" class="checkbox" @onchange="eventArgs => { CheckboxClicked(role.Description, eventArgs.Value); }"/>
                                                </div>


                                                <div class="float-child">
                                                    <p>Application Role - </p>
                                                    <br/>
                                                    <p>
                                                        <b>@role.Description</b>
                                                    </p>
                                                    <br/>
                                                </div>

                                            </div>

                                            AppOwnerCommonName = role.CommonName;
                                        }
                                    }
                                </div>
                            </div>
                        </div>
                    </div>

CSS CSS

<style>
                        .text-center-middle { margin-left: 58px; 
                        }
                        .checkbox {
                            appearance: none;
                            border: 2px solid #ccc;
                            border-color: red;
                            border-radius: 20px;
                            cursor: pointer;
                            height: 24px;
                            outline: none;
                            position: relative;
                            transition: 0.3s;
                            width: 37px;
                        }

                        .checkbox::before {
                            background: red;
                            border-radius: 50%;
                            content: "";
                            height: 15px;
                            left: 0px;
                            position: absolute;
                            top: 2px;
                            transition: 0.3s ease-in-out;
                            width: 15px;
                        }

                        .checkbox:checked::before {
                            background: green;
                            transform: translateX(18px);
                        }

                        .checkbox:checked { border-color: green; }

                        .float-container {
                            border: 3px solid #fff;
                            padding: 20px;
                        }

                        .float-child {
                            border: 2px solid red;
                            padding: 20px;
                            text-align-last: center;
                            width: 100%;
                        }

                        .float-child-unknown {
                            border: 2px solid red;
                            padding: 20px;
                            text-align-last: center;
                            width: 100%;
                        }

                        .box-color-change {
                            border: 2px solid seagreen;
                            padding: 20px;
                            text-align-last: center;
                            width: 100%;
                        }
                    </style>

You absolutely don't need JavaScript for that!你绝对不需要 JavaScript!

There's a trick to style form elements however you dream of.无论您梦想如何,都有一个技巧可以为表单元素设置样式。 You just need to put your element right after the native input and wrap both with <label> to make it clickable:您只需要将您的元素放在本机输入之后,并用<label>包裹两者以使其可点击:

<label>
    <input type="checkbox" class="checkbox-native">
    <i class="checkbox-custom"></i>
</label>

Then, you can change the appearance of your custom element when the native checkbox is checked:然后,您可以在选中本机复选框时更改自定义元素的外观:

.checkbox-native {
    display: none;
}

.checkbox-custom {
    display: inline-block;
    /* some general style */
}

:checked + .checkbox-custom {
    /* style only applicable when checked */
}

You don't need any JS or jumping through hoops backwards CSS.您不需要任何 JS 或向后跳过 CSS。

You're creating a custom control, so build a component to represent it.您正在创建自定义控件,因此请构建一个组件来表示它。 I've not included your Css etc to keep the answer short, just used standard Bootstrap.我没有包括你的 Css 等以保持答案简短,只是使用了标准的 Bootstrap。

MyCheckBox.razor

<span class="p-2 border @borderColor">
    <input type="checkbox" checked="@_currentValue" @onchange="Changed" />
</span>

@code {
    [Parameter] public bool Value { get; set; }
    [Parameter] public EventCallback<bool> ValueChanged { get; set; }

    private string borderColor => _currentValue ? "border-success" : "border-danger";
    private bool _currentValue;

    protected override void OnInitialized()
    => _currentValue = this.Value;

    private async Task Changed(ChangeEventArgs e)
    {
        _currentValue = (bool)e.Value;
        if (ValueChanged.HasDelegate)
            await ValueChanged.InvokeAsync(_currentValue);
    }
}

MyCheckBox.razor.css

/*Add your custom css here */

Test page测试页

@page "/"
<div class="m-2 p-2">
    <MyCheckBox Value="value" ValueChanged="this.ValueChanged"></MyCheckBox>
</div>
<div class="m-2 p-2">
    @this.value
</div>

@code {
    bool value;

    private void ValueChanged(bool value)
        {
        this.value = value;
        }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM