简体   繁体   English

MVC / Bootstrap - 组中的单选按钮未在初始化时选择

[英]MVC / Bootstrap - Radio Button in group not selecting on initialisation

I have an edit form in my MVC application that has a Traffic Light Status. 我的MVC应用程序中有一个具有交通灯状态的编辑表单。

When the form is initially opened, the check mark on the "lights" always set to "green" despite jQuery code to set otherwise. 最初打开表单时,“灯”上的复选标记始终设置为“绿色”,尽管设置了jQuery代码。

All the code is firing, but it just does not set the initial state. 所有代码都在触发,但它没有设置初始状态。

In this simplified example, if the Model's AlertLevel is set to "Red", the "tick" is still on the "green" 在这个简化的例子中,如果Model的AlertLevel设置为“Red”,则“tick”仍然在“green”上

Markup 标记

@Html.HiddenFor(model => model.AlertLevel)
<span class="btn-group RAGStatus" data-toggle="buttons">
    <label class="btn btn-success active " style="width:50px; height: 30px">
        <input type="radio" name="ragStatus" id="ragButton_Green" autocomplete="off" value="Green">
        <span class="glyphicon glyphicon-hidden unchecked"></span> <span class="glyphicon glyphicon-ok checked"></span>
    </label>
    <label class="btn btn-warning" style="width:50px; height: 30px">
        <input type="radio" name="ragStatus" id="ragButton_Amber" autocomplete="off" value="Amber">
        <span class="glyphicon glyphicon-hidden unchecked"></span> <span class="glyphicon glyphicon-ok checked"></span>
    </label>
    <label class="btn btn-danger" style="width:50px; height: 30px">
        <input type="radio" name="ragStatus" id="ragButton_Red" autocomplete="off" value="Red">
        <span class="glyphicon glyphicon-hidden unchecked"></span> <span class="glyphicon glyphicon-ok checked"></span>
    </label>
</span>

jQuery jQuery的

$(function () {
    var status = $("#AlertLevel").val();
    $('#ragButton_' + status).prop("checked", true);
})

Model 模型

public class DeptStatus
{
    public string AlertLevel { get; set; }
}

CSS (for the glyph icon display) CSS (用于字形图标显示)

.RAGStatus .checked {
    display: none;
}

.RAGStatus .active .checked {
    display: inline-block;
}

.RAGStatus .active .unchecked {
    display: none;
}

Update 更新

In a follow on jQuery function (removed from example), if I do var ragStatus = $('input[name=ragStatus]:checked').val(); var ragStatus = $('input[name=ragStatus]:checked').val(); jQuery函数中(从示例中删除),如果我执行var ragStatus = $('input[name=ragStatus]:checked').val(); then it returns "Red" 然后它返回“红色”

You shouldn't need to rely on jQuery to select the initial value of the button group as razor will be able to do this for you, so in your case: 您不应该依赖jQuery来选择按钮组的初始值,因为razor将能够为您执行此操作,因此在您的情况下:

<span class="btn-group RAGStatus" data-toggle="buttons">
    <label class="btn btn-success active " style="width:50px; height: 30px">
        @Html.RadioButtonFor(m => m.AlertLevel, "Green", new { autocomplete = "off" })
        <span class="glyphicon glyphicon-hidden unchecked"></span> <span class="glyphicon glyphicon-ok checked"></span>
    </label>
    <label class="btn btn-warning" style="width:50px; height: 30px">
        @Html.RadioButtonFor(m => m.AlertLevel, "Amber", new { autocomplete = "off" })
        <span class="glyphicon glyphicon-hidden unchecked"></span> <span class="glyphicon glyphicon-ok checked"></span>
    </label>
    <label class="btn btn-danger" style="width:50px; height: 30px">
        @Html.RadioButtonFor(m => m.AlertLevel, "Red", new { autocomplete = "off" })
        <span class="glyphicon glyphicon-hidden unchecked"></span> <span class="glyphicon glyphicon-ok checked"></span>
    </label>
</span>

Looks like bootstrap adds a active class to the container. 看起来像bootstrap将一个active类添加到容器中。

try 尝试

$(function () {
    var status = $("#AlertLevel").val();
    $('#ragButton_' + status).trigger('click');
})

What if you add: 如果您添加以下内容:

$('#ragButton_' + status).attr('checked', 'checked);

It might have some problems with prop function due to version of jQuery. 由于jQuery的版本,它可能在prop函数方面存在一些问题。

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

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