【问题标题】:c# radio button bound to model; checked state styling cssc# 绑定到模型的单选按钮;检查状态样式 css
【发布时间】:2016-12-17 03:45:57
【问题描述】:

我正在尝试使用 css 为这些单选按钮设置“选中状态”的样式。除了选中之外,我已经设置了所有其他状态的样式,并且我已经对将它们切换到 input type="radio" 进行了一些挖掘,因为它们看起来更容易设置样式,但还没有找到将它们绑定到模型的方法。见以下代码:

<div class="form-group">
     <div class="btn-group" id="roles_select" role="group" data-toggle="buttons">
       @Html.LabelFor(model => model.recipientTypeId, "Assign Notification To: ", htmlAttributes: new { @class = "control-label col-md-5" })
                <label class="btn btn-primary roles" id="admin">
                    @Html.RadioButtonFor(model => model.recipientTypeId, "2")Admin
                </label>
                <label class="btn btn-primary roles" id="candidate">
                    @Html.RadioButtonFor(model => model.recipientTypeId, "1")Candidate
                </label>
                <label class="btn btn-primary col-md-push-0 roles" id="manager">
                    @Html.RadioButtonFor(model => model.recipientTypeId, "3")Manager
                </label>
      </div>
</div>

来自控制器的代码:

public ActionResult Create()
    {

        ViewBag.recipientTypeId = new SelectList(db.RecipientType, "Id", "recipientRole");
        return View();
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit([Bind(Include = "Id,notificationType1,recipientTypeId,frequency")] NotificationType notificationType)
    {
        if (ModelState.IsValid)
        {
            db.Entry(notificationType).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Create");
        }
        ViewBag.recipientTypeId = new SelectList(db.RecipientType, "Id", "frequency", "recipientRole", notificationType.recipientTypeId);
        return View(notificationType);
    }

CSS 被剪断(不工作)

#roles_select input[type="radio"]:checked + label {
background-color:#fff;
}

【问题讨论】:

  • 模型绑定和 css 完全不相关。如果它没有约束力,那么它是由于您的代码的其他问题。向您展示模型和控制器代码。
  • input[type="radio"]:checked + label 表示 &lt;label&gt; 元素的样式**紧跟在 **&lt;radio checked /&gt; 元素之后(您的单选按钮被标签包裹 - 而不是之后)

标签: c# html css asp.net-mvc twitter-bootstrap


【解决方案1】:

您可以使用jquery 执行此操作。

您只需将change 事件处理程序触发到radio 按钮即可。

$('input[type="radio"]').on('change', function() {
     $('input[type="radio"]').removeClass('radioChecked');
     $(this).addClass('radioChecked'); 
});

你的 CSS:

.radiocheck { color: black; background-color:black; }

这是我的示例:jsfiddle

【讨论】:

  • 不幸的是没有工作。这是使用上述类语法作为测试尝试的 css:`.radiocheck:checked { color: black;背景颜色:黑色; }'
  • 样式有效,但未绑定到我的模型,因此选择不会输入到数据库中
  • 您可以从服务器端发布您的功能吗?
  • 你不需要 jquery ——你可以使用input[type=checkbox]:checked { ... }来设置样式
  • 是的,我知道。
猜你喜欢
  • 2013-09-20
  • 2018-07-23
  • 1970-01-01
  • 2013-06-29
  • 2021-07-19
  • 1970-01-01
  • 1970-01-01
  • 2012-12-15
  • 1970-01-01
相关资源
最近更新 更多