我制作了一个学校网站,教师可以在其中上传学生的分数。到目前为止我有这三个文件:
第一个,将根据学科和类(class)显示每个学生的姓名
<table class="check">
<tr class="arrow">
<th>Student</th>
<th>Add Mark</th>
</tr>
@foreach(var users in user){
<tr class="arrow">
<td>@users.Nombre @users.Apellido</td>
<td>
<form method="post">
<input type="text" id="user" style="display: none" name="user" @Validation.For("nombre") value="@users.UserId" />
<input type="text" id="galleryId" style="display: none" name="galleryId" @Validation.For("nombre") value="@galleryId" />
<input type="text" id="note" name="button2" name="note" @Validation.For("nombre") />
<input type="button" value="Ready" title="Ready" onclick="loco(document.getElementById('user').value, document.getElementById('galleryId').value, document.getElementById('note').value)" />
</form>
</td>
</tr>
}
</table>
在这里你可以看到我使用了foreach
来显示每个学生的名字,然后在它旁边应该显示一个输入文本框,供老师写下特定学生的分数。这就是为什么我将表单包含在 foreach
中。
接下来是 ajax 文件:
function loco(user, gallery, note) {
var xmlhttp;
var user = document.getElementById("user").value;
var galleryid = document.getElementById("galleryId").value;
var note = document.getElementById("note").value;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else {// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET", "/Marks/add1/" + gallery + "/" + user + "/" + note, true);
xmlhttp.send();
}
最后,我们有了将标记插入数据库的页面,无需返回任何表或 div,只需上传标记即可。
@{
var db2 = Database.Open("ica");
var user = UrlData[1];
var galleryId = UrlData[0];
var note = UrlData[2].AsInt();
db2.Execute("INSERT INTO Notas (Nota, UserId, Sub_Id) VALUES (@0, @1, @2)", note, user, galleryId);
}
那么,为什么 ajax 将第一个学生的值发送到上传文件,而不是第二个、第三个等?为什么当我点击第二个学生的提交按钮时,它会再次发送第一个学生的分数,而且只发送第一个学生的分数?
最佳答案
您在 for-each 循环内的 HTML 中使用硬编码 ID,因此您将在页面上获得具有相同 ID 的多个元素 user
、 galleryId
和 注释
。这意味着无论您实际尝试使用哪个,您的选择器都只能选择第一个。您需要做一些事情,例如在 ID 末尾添加索引号,而不是完全硬编码 ID,以便可以将它们相互区分。
关于c# - ajax只发送输入的第一个值而不发送其他值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23618809/