[英]How Do I use jquery to get each value from a cshtml foreach loop
I am building a staff appraisal system, where users are asked 10 questions in order to appraise an employee. 我正在建立一个员工评估系统,该系统会向用户询问10个问题,以便对员工进行评估。 Something like this 像这样
Each star is assigned a value between 1 - 5. I am currently getting each value, but how do I assign each value to a variable like: var questionOne = 2; var questionTwo = 3;
每颗星都被分配一个介于1到5之间的值。我目前正在获取每个值,但是如何将每个值分配给一个变量,例如: var questionOne = 2; var questionTwo = 3;
var questionOne = 2; var questionTwo = 3;
This is my code so far: 到目前为止,这是我的代码:
<h4>Score Ola using our rating form below.</h4>
<p>Job Title: Nanny</p>
<p style="color: red; font-size: 12px;">
Note: Questions are selected based on job title. We are constantly reviewing the list of job specific questions, please assist by recommending questions you would like to add to our job specific questionaire <a href="#">here</a>
</p>
@foreach (var question in Model.AppraisalQuestions)
{
<p>@question.QuestionDescription</p>
<div class="row lead evaluation">
<div id="colorstar" class="starrr ratable"></div>
<span id="count">0</span> star(s) - <span id="meaning"> </span>
</div><br />
}
The Javascript so far: 到目前为止的Javascript:
$(document).ready(function() {
var correspondence = ["", "Poor", "Below Expectation", "Above Expectation", "Good", "Excelent" ];
$('.ratable').on('starrr:change', function(e, value) {
$(this).closest('.evaluation').children('#count').html(value);
$(this).closest('.evaluation').children('#meaning').html(correspondence[value]);
var currentval = $(this).closest('.evaluation').children('#count').html();
var target = $(this).closest('.evaluation').children('.indicators');
target.css("color","black");
target.children('.rateval').val(currentval);
target.children('#textwr').html(' ');
alert(value);
});
I want to store each score in a database. 我想将每个分数存储在数据库中。 Any help will be highly appreciated. 任何帮助将不胜感激。 Thanks 谢谢
I may be misunderstaing your question completely but this pseudo code works for me. 我可能完全误解了您的问题,但是此伪代码对我有用。 I added five empty stars to each question and assigned value and questionID to each. 我为每个问题添加了五个空星,并为每个问题分配了值和questionID。 You should be able to change the star icon via jQuery too but I didn´t include that. 您也应该能够通过jQuery更改星形图标,但我没有添加它。
Just let me know if I am misunderstanding. 如果我误会了,请告诉我。
Markup code: 标记代码:
@foreach (var question in Model.AppraisalQuestions)
{
<p>@question.QuestionDescription</p>
<div class="row lead evaluation">
<i class="glyphicon glyphicon-star-empty starrr ratable" data-value="1" data-question="@question.QuestionID"></i>
<i class="glyphicon glyphicon-star-empty starrr ratable" data-value="2" data-question="@question.QuestionID"></i>
<i class="glyphicon glyphicon-star-empty starrr ratable" data-value="3" data-question="@question.QuestionID"></i>
<i class="glyphicon glyphicon-star-empty starrr ratable" data-value="4" data-question="@question.QuestionID"></i>
<i class="glyphicon glyphicon-star-empty starrr ratable" data-value="5" data-question="@question.QuestionID"></i><br />
<span class="count">0</span> star(s) - <span class="meaning"> </span>
</div><br />
}
Javascript: Javascript:
<script type="text/javascript">
$(document).ready(function () {
var correspondence = ["", "Poor", "Below Expectation", "Above Expectation", "Good", "Excelent"];
$(".ratable").click(function() {
var value = $(this).data("value");
var questionID = $(this).data("question");
$(this).closest('.evaluation').children('.count').text(value);
$(this).closest('.evaluation').children('.meaning').html(correspondence[value]);
//TODO: send value and questionID to server, perhaps via ajax or something
});
});
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.