MVC / 引导 - 组中的单选按钮在初始化时未选择
MVC / Bootstrap - Radio Button in group not selecting on initialisation
我的 MVC 应用程序中有一个具有红绿灯状态的编辑表单。
当表单最初打开时,"灯"上的复选标记始终设置为"绿色",尽管jQuery代码另有设置。
所有代码都在触发,但它只是没有设置初始状态。
在此简化示例中,如果模型的 AlertLevel 设置为"红色",则"勾号"仍位于"绿色"上
标记
@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
$(function () {
var status = $("#AlertLevel").val();
$('#ragButton_' + status).prop("checked", true);
})
型
public class DeptStatus
{
public string AlertLevel { get; set; }
}
CSS(用于字形图标显示)
.RAGStatus .checked {
display: none;
}
.RAGStatus .active .checked {
display: inline-block;
}
.RAGStatus .active .unchecked {
display: none;
}
更新
在jQuery函数的后续(从示例中删除)中,如果我这样做var ragStatus = $('input[name=ragStatus]:checked').val();
那么它返回"红色"
您不需要依赖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>
看起来引导程序将active
类添加到容器中。
尝试
$(function () {
var status = $("#AlertLevel").val();
$('#ragButton_' + status).trigger('click');
})
如果你添加:
$('#ragButton_' + status).attr('checked', 'checked);
由于jQuery的版本,它可能prop
函数存在一些问题。
相关文章:
- 初始化ng模型时,Angular ui选择占位符不起作用
- 当使用Materialize和Meteor时,我应该在哪里初始化选择
- 选择要初始化精细上传器的文件输入
- 如何初始化Twitter引导程序表单助手日期选择器
- MVC / 引导 - 组中的单选按钮在初始化时未选择
- j查询选择菜单未初始化
- 初始化引导日期选择器后调用 noConflict()
- 如何重新初始化引导日期选择器
- 使用 PHP 变量初始化选择标签
- 使用 AngularJS 和 ng-repeat初始化选择
- 启动日期选择器格式在初始化时不起作用
- 如何访问动态生成的选择列表的表单元素值;s在初始化时隐藏
- 位置选择器无法在谷歌地图初始化中工作
- JS闭包——依赖回调的多个日期选择器初始化/调用
- 用“日期”初始化的值是什么?日期选择器的beforeShowDay函数中的参数
- 选择插件初始化
- 使.close选择填充来初始化脚本
- Jquery选择框脚本没有自动初始化
- 材质选择初始化在vue.js
- 如何处理剑道UI MVVM日期选择器初始化