大家好。默认颜色为“initialColor”:“#8e44ad”。是否可以做一些事情,如果数据库已经有颜色 @Model.Color !="" 那么它是从那里获取的,否则默认设置为 '#8e44ad' 视图
@using (Html.BeginForm("SaveUploadedFiles", "TrainingType", FormMethod.Post, new
{ enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="container">
<div class="row">
<div class="col-sm-4">
@*pic*@
@Html.DisplayNameFor(model => model.Color)
<input type="hidden" class="form-control" value="@Model.Id"
name="Picker1">
<div class="picker" id="picker1"></div>
<input type="hidden" id="colorType" name="color" value="#8E44AD" />
@*pic*@
<script>
$("#picker1").colorPick({
'initialColor': '#8e44ad',
'palette': ["#1abc9c", "#16a085", "#2ecc71", "#27ae60", "#3498db", "#2980b9", "#9b59b6", "#8e44ad", "#34495e", "#2c3e50", "#f1c40f", "#f39c12", "#e67e22", "#d35400", "#e74c3c", "#c0392b", "#ecf0f1"],
'onColorSelected': function () {
console.log("The user has selected the color: " + this.color);
this.element.css({ 'backgroundColor': this.color, 'color': this.color });
}
});
</script>
<button type="submit" class="btn btn-primary">Сохранить</button>
@Html.ActionLink("Назад", "Index", "", new { @class = "btn btn-primary" })
</div>
</div>
</div>
}
js
$("#picker1").colorPick({
'initialColor': '#8e44ad',
'palette': ["#1abc9c", "#16a085", "#2ecc71", "#27ae60", "#3498db", "#2980b9", "#9b59b6", "#8e44ad", "#34495e", "#2c3e50", "#f1c40f", "#f39c12", "#e67e22", "#d35400", "#e74c3c", "#c0392b", "#ecf0f1"],
'onColorSelected': function() {
console.log("The user has selected the color: " + this.color);
this.element.css({
'backgroundColor': this.color,
'color': this.color
});
}
});
试图这样做
var initialColor = @Model.Color;
$("#picker1").colorPick({
get initialColor() {
return initialColor;
},
set initialColor(value) {
initialColor = value;
},
'palette': ["#1abc9c", "#16a085", "#2ecc71", "#27ae60", "#3498db", "#2980b9", "#9b59b6", "#8e44ad", "#34495e", "#2c3e50", "#f1c40f", "#f39c12", "#e67e22", "#d35400", "#e74c3c", "#c0392b", "#ecf0f1"],
'onColorSelected': function() {
console.log("The user has selected the color: " + this.color);
this.element.css({
'backgroundColor': this.color,
'color': this.color
});
}
});
不工作。有人可以提出解决方法吗?
由 Aarnihauta 回答 查看评论