简体   繁体   English

如何使用jquery从cshtml foreach循环获取每个值

[英]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.

相关问题 如何使用 jquery 从 foreach 循环中获取每个选定的复选框值 - how to get each selected checkbox value from foreach loop using jquery 如何在PHP foreach循环中使用jQuery .each()? - How to use jQuery .each() in PHP foreach loop? 如何使用jquery和codeigniter从foreach循环中获取当前值 - How to get the current value from the foreach loop using jquery and codeigniter 如何从 forEach 循环中取出元素 - how do I get an element out from the forEach loop 如何使用选择器在jQuery的foreach循环中定位每个孩子 - How to use selector to locate each child in a foreach loop in Jquery 我怎样才能让我的jQuery函数可用于foreach循环中的每个元素? - How can I get my jQuery function for work for each element inside a foreach loop? 如何使用 forEach 循环到 append 为每个 class 一个新的 div? - How do I use a forEach loop to append a new div to each class? 如何获取每个键的值并使用jQuery在变量中使用它? - How do I get the value of each key pressed and use it in a variable with jQuery? 如果内部元素不同,如何从每个循环为数组获取值 - how do i get value to my array from each loop if the inner elements were different 如何获取在cshtml中使用循环创建的每个div / li / a的特定元素的Id - How can I get the Id of the particular element of each div / li / a that is created using loop in cshtml
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM