[英]Using javascript/jquery from within razor code
It seems simple but i cant figure out how to call javascript function from within razor code. 似乎很简单,但我无法弄清楚如何从剃刀代码中调用javascript函数。
Problem: I need to get the position of a column in my table header with the id passed.. I am calling getPosition function from within my razor code 问题:我需要在表头中获得传递ID的列的位置。.我正在从剃刀代码中调用getPosition函数
<table>
<thead>
<tr>
@foreach (Assessment geAssessment in Model.assessments)
{
<th id=@geAssessment.AssessmentID>@geAssessment.Name</th>
}
</tr>
</thead>
<tbody>
<tr>
@foreach (ShortResult geResult in Model.results)
{
@:{ var i = getPosition(@geResult.assessmentId);}
}
</tr>
</tbody>
</table>
My script in the same view/page 我的脚本在同一视图/页面中
<script type="text/javascript"> function getPosition(id) { var c = '#' + id; alert(c); return $c.index(); } $(function () {}); </script>
UPDATE UPDATE
As suggested by Max, i changed my table as follows, which is perfect but now how can i set a value in the td 正如Max所建议的,我如下更改了表格,这很完美,但是现在我如何在td中设置一个值
<tbody>
<tr>
@{
var index = 4; //start index of assessments will be 4
foreach(Assessment geAssessment in Model.assessments)
{
<td>
@foreach (ShortResult geResult in Model.results)
{
if(geResult.StudentID == geStudent.studentid)
{
@:
<script>
{
var assessmentIndex = getPosition(@geResult.assessmentId);
@*if (assessmentIndex == @index) {
geResult.ResultValue
}*@
}
</script>
}
}
</td>
index++;
}
}
</tr>
</tbody>
Now let me explain whats going on .. 现在让我解释一下发生了什么..
for each td i check if i have a result with this particular assessmentid, if yes i want to print it in the td..there is some syntax error here: 对于每个td,我检查是否有带有此特定评估id的结果,如果是,我想在td中打印它。.此处存在一些语法错误:
if (assessmentIndex == @index) { geResult.ResultValue }
Your script with getPosition function has to be above your call. 具有getPosition函数的脚本必须位于调用上方。 After @: you have to use directive otherwise it's considered like a text. @之后:您必须使用指令,否则将其视为文本。 Your code has to look like: 您的代码必须看起来像:
<script type="text/javascript">
function getPosition(id) {
var c = '#' + id;
alert(c);
return $c.index();
}
$(function () {});
</script>
<table>
<thead>
<tr>
@foreach (Assessment geAssessment in Model.assessments)
{
<th id=@geAssessment.AssessmentID>@geAssessment.Name</th>
}
</tr>
</thead>
<tbody>
<tr>
@foreach (ShortResult geResult in Model.results)
{
@: <script>{ var i = getPosition(@geResult.assessmentId);}</script>
}
</tr>
</tbody>
</table>
Hi there is no need of @: syntax you can directly use it so now your code look like. 嗨,您不需要@:语法,您可以直接使用它,因此现在您的代码看起来像。
<script type="text/javascript">
function getPosition(id) {
var c = '#' + id;
alert(c);
return $c.index();
}
$(function () {});
</script>
<table>
<thead>
<tr>
@foreach (Assessment geAssessment in Model.assessments)
{
<th id=@geAssessment.AssessmentID>@geAssessment.Name</th>
}
</tr>
</thead>
<tbody>
<tr>
@foreach (ShortResult geResult in Model.results)
{
<script>{ var i = getPosition(@geResult.assessmentId);}</script>
}
</tr>
</tbody>
</table>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.