将JavaScript与数据库表中的值相结合
Combining JavaScript with Values from Database Table
我使用的是MVC和实体框架。我的HTML:中有两个小表格
<div class="col-md-4">
<table class="table orioles-table" style="background-color:white; opacity: 0.9">
<tr>
<th style="text-align:center;">
<img class="buck-picture" src="/*pic*/" />
<br />
Baltimore Orioles
<br />
@Html.ActionLink("Add Win", "Edit", "Teams", new { id = 2 }, null)
</th>
</tr>
<tr>
@foreach (var item in Model.Where(x => x.TeamName == "Baltimore Orioles"))
{
<td id="orioles-wins" class="text-center">
@Html.DisplayFor(modelItem => item.SeriesWins) Series Wins
</td>
}
</tr>
</table>
</div>
<div class="col-md-4">
<table class="table redSox-table" style="background-color: white; opacity: 0.9">
<tr>
<th style="text-align: center;">
<img class="picture" src="/*pic*/" />
<br />
Boston Red Sox
<br />
@Html.ActionLink("Add Win", "Edit", "Teams", new { id = 1 }, null)
</th>
<tr>
@foreach (var item in Model.Where(x => x.TeamName == "Boston Red Sox"))
{
<td id="redsox-wins" class="text-center">
@Html.DisplayFor(modelItem => item.SeriesWins) Series Wins
</td>
}
</tr>
</table>
</div>
现在,我的JS技能非常有限。。。但这是我的问题。。如何将数据库中表中的值转换为JavaScript,以便执行条件语句?例如,如果红袜队的系列赛胜利比黄莺队多。。。使文本变大变红(这只是我希望JS做什么的一个例子)。我该怎么用JS写呢?
我再次为你的草率/困惑道歉,但如果你有问题,我会尽力回答。
感谢您的帮助。
更新:
<table class="table orioles-table" style="background-color:white; opacity: 0.9">
<tr>
<th style="text-align:center;">
<img class="buck-picture" src="~/Images/buck-showalter_pointing-sidebar.gif" />
<br />
Baltimore Orioles
<br />
@Html.ActionLink("Add Win", "Edit", "Teams", new { id = 2 }, null)
</th>
</tr>
<tr>
@foreach (var item in Model.Where(x => x.TeamName == "Baltimore Orioles"))
{
<td class="text-center">
<span id="redsoxLabel">
@* @ViewBag.RedSox*@ 1
</span>
</td>
}
</tr>
</table>
<table class="table redSox-table" style="background-color: white; opacity: 0.9">
<tr>
<th style="text-align: center;">
<img class="picture" src="~/Images/cartoonMe.PNG" />
<br />
Boston Red Sox
<br />
@Html.ActionLink("Add Win", "Edit", "Teams", new { id = 1 }, null)
</th>
<tr>
@foreach (var item in Model.Where(x => x.TeamName == "Boston Red Sox"))
{
<td class="text-center">
<span id="oriolesLabel">
@*@ViewBag.Orioles*@ 5
</span>
</td>
}
</tr>
</table>
CSS:
.big-red-text{
color: red;
font-size: 50px;
}
JS:
$(document).ready(function () {
var Orioles = document.getElementById("redsoxLabel");
var RedSox = document.getElementById("oriolesLabel");
if (Orioles.innerText > RedSox.innerText) {
document.getElementById("redSoxLabel").className = "big-red-text";
}
if (RedSox.innerText > Orioles.innerText) {
document.getElementById("oriolesLabel").className = "big-red-text";
}
})
您可以让javascript引用您的模型,如下所示:
var property = "@Model.Property";
然而,使用剃须刀,您也可以根据您的型号和条件应用样式(即大红色文本)。您不一定要依赖javascript。例如,当使用您的模型时,您可以设置变量来存储redsox胜利和orioles胜利。。。然后根据这些变量的条件设置类。
{
<span class="@(redSoxWins > oriolesWins) ? "big-red-text" : "")">
}
但是,如果您更喜欢单独使用javascript访问值。。。您可以使用选择器访问这些值。
var redsoxWins = document.getElementById("redsox-wins");
var oriolesWins = document.getElementById("orioles-wins");
if(redsoxWins>oriolesWins){
document.getElementById("redSoxLabel").className = "big-red-text";
}
相关文章:
- 媒体查询与Javascript相结合
- iframe与Javascript相结合的zipcode检查器
- 将n个对象与javascript相结合
- 将JavaScript与数据库表中的值相结合
- 将 Python 字符串与 javascript 值相结合
- 将文本与选择相结合(JavaScript)
- 如何将PHP的DOMDocument与JavaScript模板相结合
- Url.Action 将以前的 URL 与 VS IDE 中 JavaScript 中的当前路由参数相结合
- 按州过滤标记与地点自动完成相结合 - Google Maps JavaScript API
- Javascript - 将多维数组与索引相结合
- 了解递归函数与 Javascript 中的逻辑运算符相结合
- 是否可以将ImageNow与Javascript和php相结合
- Javascript字符串操作,将数字与字符串相结合
- HTML5表单验证与javascript篮子验证相结合
- Form Submit与Javascript相结合
- 将Javascript手风琴式菜单效果与jQuery可过滤照片库相结合
- 将图像数组与javascript(带或不带画布)相结合
- 将javascript和css文件与ASP.NET MVC中的Rejicer相结合
- 将JavaScript加载与命名空间相结合
- 将PHP循环与随机图像(javascript)代码相结合