我看到很多与此问题相关的问题。但这些问题与我的场景无关。
下面是我在 JSP 页面中的单选按钮设计。为了在标签中添加 active 类,我使用了 JSTL 标签。
<div class="btn-group" data-toggle="buttons">
<label class="btn btn-primary <c:if test=" ${initialExamScreenTwoForm.cervicalSpineHeadTiltRight==1} ">active</c:if>">
<form:checkbox path="cervicalSpineHeadTiltRight" id="cervicalSpineHeadTiltRight_id" value="1" autocomplete="off" />Right
</label>
<label class="btn btn-primary <c:if test=" ${initialExamScreenTwoForm.cervicalSpineHeadTiltLeft==1} ">active</c:if>">
<form:checkbox id="cervicalSpineHeadTiltLeft_id" path="cervicalSpineHeadTiltLeft" value="1" autocomplete="off" />Left
</label>
<label class="btn btn-primary <c:if test=" ${initialExamScreenTwoForm.cervicalSpineHeadTiltNormal==1} ">active</c:if>">
<form:checkbox id="cervicalSpineHeadTiltNormal_id" path="cervicalSpineHeadTiltNormal" value="1" autocomplete="off" />Normal
</label>
</div>
active 类添加得很好。但选中的属性不会添加到单选按钮。
从后端加载时它工作正常。当我选择其他值时,在前端它显示为选定的(即添加了事件类),但选中的属性未添加到按钮。因此它将旧值发送到后端。
在 Bootstrap 中,像这样使用
c.prototype.toggle = function() {
var a = !0,
b = this.$element.closest('[data-toggle="buttons"]');
if (b.length) {
var c = this.$element.find("input");
"radio" == c.prop("type") && (c.prop("checked") && this.$element.hasClass("active") ? a = !1 : b.find(".active").removeClass("active")), a && c.prop("checked", !this.$element.hasClass("active")).trigger("change")
}
a && this.$element.toggleClass("active")
};
我不知道问题出在哪里。请帮我找出我做错了什么。
最佳答案
问题出在bootstrap.js版本(我使用的是3.2.0)。我无法更新完整代码,因为我为我的项目添加了附加代码。
所以我更新了 bootstrap.min.js 中的 data-toggle="buttons" 部分,它工作正常。我更改的代码是。
c.prototype.toggle = function() {
var a = !0,
b = this.$element.closest('[data-toggle="buttons"]');
if (b.length) {
var c = this.$element.find("input");
"radio" == c.prop("type") ? (c.prop("checked") && (a = !1), b.find(".active").removeClass("active"), this.$element.addClass("active")) : "checkbox" == c.prop("type") && (c.prop("checked") !== this.$element.hasClass("active") && (a = !1), this.$element.toggleClass("active")), c.prop("checked", this.$element.hasClass("active")), a && c.trigger("change")
} else this.$element.attr("aria-pressed", !this.$element.hasClass("active")), this.$element.toggleClass("active")
};
var d = a.fn.button;
a.fn.button = b, a.fn.button.Constructor = c, a.fn.button.noConflict = function() {
return a.fn.button = d, this
}, a(document).on("click.bs.button.data-api", '[data-toggle^="button"]', function(c) {
var d = a(c.target);
d.hasClass("btn") || (d = d.closest(".btn")), b.call(d, "toggle"), a(c.target).is('input[type="radio"]') || a(c.target).is('input[type="checkbox"]') || c.preventDefault()
}).on("focus.bs.button.data-api blur.bs.button.data-api", '[data-toggle^="button"]', function(b) {
a(b.target).closest(".btn").toggleClass("focus", /^focus(in)?$/.test(b.type))
})
}
谢谢@kp辛格
关于javascript - Bootstrap 数据切换单选按钮/复选框选中属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35169643/