这是我的困境:我有一个带有 if 语句的 jQuery 代码,它根据用户填写表单的页面上选择的值将特定类应用于元素。我还构建了另一个表单,用户可以在其中查看已填写的表单的详细信息。显示详细信息页面时,应用了大部分类,但灰色的“只读”背景覆盖了原始类的背景颜色。如果可以避免的话,我真的不想进入引导文件并调整只读背景颜色代码。如何将完整的类(包括背景颜色)应用于只读元素?
“创建”页面的代码片段:
<div class="col-md-12">
<div class="col-md-10">
@Html.LabelFor(model => model.Question1, htmlAttributes: new { @class = "control-label" })
</div>
<div class="col-md-2">
@Html.DropDownListFor(model => model.Question1, new[] { new SelectListItem() { Text = "Pass", Value = "Pass" }, new SelectListItem() { Text = "Fail", Value = "Fail" }, new SelectListItem() { Text = "NA", Value = "NA" } }, "Select an option", htmlAttributes: new { @class = "form-control text-box centerline", @onchange = "updateEscalationsListeningScore()", id = "question1Answer" })
@Html.ValidationMessageFor(model => model.Question1, "", new { @class = "text-danger" })
</div>
</div>
来自“创建”页面的 jQuery:
if (Q1Answer === "Pass") {
$('#question1Answer').addClass("correctListeningAnswer").removeClass("incorrectListeningAnswer").removeClass("naListeningAnswer");
Q1Score = 3;
Q1PotentialPoints = 3;
}
else if (Q1Answer === "Fail") {
$('#question1Answer').addClass("incorrectListeningAnswer").removeClass("correctListeningAnswer").removeClass("naListeningAnswer");
Q1Score = 0;
Q1PotentialPoints = 3;
}
else if (Q1Answer === "NA") {
$('#question1Answer').addClass("naListeningAnswer").removeClass("incorrectListeningAnswer").removeClass("correctListeningAnswer");
Q1Score = 0;
Q1PotentialPoints = 0;
}
else {
$('#question1Answer').removeClass("correctListeningAnswer").removeClass("incorrectListeningAnswer").removeClass("naListeningAnswer");
Q1Score = 3;
Q1PotentialPoints = 3;
}
“详细信息”页面中的代码片段:
<div class="col-md-12">
<div class="col-md-10">
@Html.LabelFor(model => model.Question1, htmlAttributes: new { @class = "control-label" })
</div>
<div class="col-md-2">
@Html.EditorFor(model => model.Question1, new { htmlAttributes = new { @class = "form-control text-box centerline", id = "question1Answer", @readonly = "readonly" } })
@Html.ValidationMessageFor(model => model.Question1, "", new { @class = "text-danger" })
</div>
</div>
来自“详细信息”页面的 jQuery:
if (Q1Answer === "Pass") {
$('#question1Answer').addClass("correctListeningAnswer").removeClass("incorrectListeningAnswer").removeClass("naListeningAnswer");
Q1Score = 3;
Q1PotentialPoints = 3;
}
else if (Q1Answer === "Fail") {
$('#question1Answer').addClass("incorrectListeningAnswer").removeClass("correctListeningAnswer").removeClass("naListeningAnswer");
Q1Score = 0;
Q1PotentialPoints = 3;
}
else if (Q1Answer === "NA") {
$('#question1Answer').addClass("naListeningAnswer").removeClass("incorrectListeningAnswer").removeClass("correctListeningAnswer");
Q1Score = 0;
Q1PotentialPoints = 0;
}
else {
$('#question1Answer').removeClass("correctListeningAnswer").removeClass("incorrectListeningAnswer").removeClass("naListeningAnswer");
Q1Score = 3;
Q1PotentialPoints = 3;
}
CSS
.correctListeningAnswer {
background-color: #c6efce;
color: #006100;}
.incorrectListeningAnswer {
background-color: #ffc7ce;
color: #9c0006;}
.naListeningAnswer {
background-color: #ffeb9c;
color: #9c5700;}
预先感谢您的帮助。
科里