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

时间:2018-05-01 08:41:06

标签: javascript c# jquery asp.net-mvc razor

我正在建立员工评估系统,向用户询问10个问题以评估员工。像这样Staff Appraisal

每个星星的分配值介于1 - 5之间。我目前正在获取每个值,但如何将每个值分配给变量,如:var questionOne = 2; var questionTwo = 3;

到目前为止,这是我的代码:

<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 />
}

到目前为止的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);
  });

我想将每个分数存储在数据库中。任何帮助将受到高度赞赏。感谢

1 个答案:

答案 0 :(得分:0)

我可能完全误解了你的问题,但这个伪代码对我有用。我为每个问题添加了五个空星,并为每个问题分配了值和questionID。您应该能够通过jQuery更改星形图标,但我没有包含它。

如果我误解,请告诉我。

标记代码:

const grid = Array.from(new Array(5),(_,x)=>Array.from(new Array(5),(_,y)=>addCell(x,y)));

使用Javascript:

const grid = Array.from(new Array(5),()=>Array.from(new Array(5),()=>"-"));

const rotate = grid => 
  grid[0].map(
    (_,y)=>grid.map(
      (_,x)=>[y,x]
    )
  ).map(
    row=>row.map(([x,y])=>grid[y][x])
  );
const format = grid => grid.map(x=>x.join(" ")).join("\n");
//set some values of grid
[[0,2],[1,2],[2,2],[3,2],[4,2]].forEach(
  ([x,y])=>grid[x][y]="X"
);

//you can map the grid to columns first, it'll look like it's rotated
//  unless you generate the columns in div float lefts
console.log("map grid columns first:")
console.log(format(grid));

//you can rotate the grid to build each row and then each column like html table
console.log("map grid rows first:")
console.log(format(rotate(grid)));