简体   繁体   English

如何通过select的当前值更改背景颜色?

[英]How to change color of background by current value of select?

Using asp-core net 2.2 mvc, I would like to change the background color of div tag by the current value of select 使用asp-core net 2.2 mvc,我想通过select的当前值更改div标签的背景颜色

I have defined the following select in a view: 我在视图中定义了以下选择:

<div id="bgcolor" class="form-group">
  <label asp-for="BackGround" class="control-label"></label>
    <select asp-for="BackGround" asp-items="Html.GetEnumSelectList<BackGroundColor>()" onchange="changeBackground">
      <option selected="selected" value="">Please select</option>
    </select>
</div>

Then I added the script: 然后我添加了脚本:

@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
<script>
    function changeBackground(value) {
        $("#bgcolor").removeClass("bg-primary bg-secondary bg-warning").addClass(value)
    };
</script>
}

The enum has the following values 枚举具有以下值

public enum BackGroundColor
{
    bg-primary,
    bg-secondary,
    bg-success,
    bg-danger,
    bg-warning,
}

I am not even sure if the script is loaded. 我什至不确定脚本是否已加载。 Could you please indicate the correct way? 您能指出正确的方法吗?

 <select asp-for="BackGround" asp-items="Html.GetEnumSelectList<BackGroundColor>()" onchange="changeBackground(this)">

 function changeBackground(data) { //put this data.value where ever you want alert(data.value); } 

  $("#bgcolor").removeClass("bg-primary bg-secondary bg-warning").addClass(data.value); 

please try. 请试试。

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

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