【发布时间】:2019-02-12 12:39:42
【问题描述】:
我想将一个 int 值(每次按下按钮时增加)存储到每个人的 .txt 文件中。
我不知道如何在单击时存储每个人的按钮值。这就是我想出的。在这方面需要帮助。
实际问题: 每个投票的人都应该能够看到总票数以及每个候选人的投票。您必须使用文件来记录投票。
@{
var dataFile = Server.MapPath(@"~/App_Data/data.txt");
string[] votesArr = File.ReadAllText(dataFile).Split(','); // your path
string toWrite = "";
for (int i = 0; i < votesArr.Length; i += 2)
{
if (votesArr[i].Equals("Harry")) // Equals here is hardcoded, replace with parameter
{
votesArr[i + 1] = "" + (Int32.Parse(votesArr[i + 1]) + 1);
}
else if (votesArr[i].Equals("John")) // Equals here is hardcoded, replace with parameter
{
votesArr[i + 1] = "" + (Int32.Parse(votesArr[i + 1]) + 1);
}
else if (votesArr[i].Equals("May")) // Equals here is hardcoded, replace with parameter
{
votesArr[i + 1] = "" + (Int32.Parse(votesArr[i + 1]) + 1);
}
else if (votesArr[i].Equals("Jane")) // Equals here is hardcoded, replace with parameter
{
votesArr[i + 1] = "" + (Int32.Parse(votesArr[i + 1]) + 1);
}
toWrite += votesArr[i] + votesArr[i + 1];
}
File.WriteAllText(dataFile, toWrite);
}
<!DOCTYPE html>
<html>
<head>
<title>Elections</title>
</head>
<body>
<p id="1">Harry</p>
<input id="1" type="submit" value="Vote Harry">
<p id="2">John</p>
<input id="2" type="submit" value="Vote John">
<p id="3">May</p>
<input id="3" type="submit" value="Vote May">
<p id="4">Jane</p>
<input id="4" type="submit" value="Vote Jane">
</body>
</html>
【问题讨论】:
-
这感觉像是作业,如果是这样,请查看meta.stackoverflow.com/questions/334822/… 以获取一些集体站点智慧
标签: javascript jquery html asp.net