【发布时间】:2020-10-05 02:23:37
【问题描述】:
我在每个产品的循环下创建了评论框。但是在我的第一个产品中,我在这里成功评论了。但是在我的第二个产品中,当我点击“添加评论”时,我发现了意想不到的结果。点击后,此评论与我的第一个产品视频评论不同。
这是我的代码:
<h4>Product Review</h4>
</br></br>
@foreach (var laptop in ViewBag.v)
{
<div class="row">
<video src="~/@laptop.Image1" alt="Card Image" class="card-img-top" controls height="400px" loop />
</div>
@:</br>
@:<div class="border">
<div id="display" class="ml-2"></div>
@:</div>
@:</br>
@:<div class="comment">
<textarea id="title" type="text " rows="2" cols="4" onkeyup="Allow()" placeholder="write a comment......" style="width:840px;height:50px;"></textarea>
<input type="submit" value="Add Comment" class="btn btn-outline-dark" onclick="insert()" style="width:150px;height:50px;" />
@:</form>
@:</div>
<div class="row float-right">
<a asp-action="Details" asp-controller="ShopShow" asp-route-id="@laptop.Id" class="btn btn-primary pull-right btn-outline-light">Details</a>
</div>
@:</br></br></hr>
}
site.js
var titles = [];
var titleInput = document.getElementById("title");
var messageBox = document.getElementById("display");
function Allow() {
if (!user.title.value.match(/[a-zA-Z]$/) && user.title.value != "") {
user.title.value = "";
alert("Please Enter only alphabets");
}
window.location.reload()
}
function insert() {
titles.push(titleInput.value);
clearAndShow();
}
function clearAndShow() {
titleInput.value = "";
messageBox.innerHTML = "";
messageBox.innerHTML += " " + titles.join("<br/> ") + "<br/>";
}
输出:
在输出上方,我的第一个评论框正在工作,但第二个评论框不工作。有什么解决办法。
【问题讨论】:
-
不确定到底是什么问题,但
var messageBox = document.getElementById("display");将始终返回与该 ID 匹配的第一个元素。我猜这会导致一些不良行为,因为您的页面上可以有多个具有该 ID 的元素
标签: javascript c# jquery asp.net-core asp.net-core-mvc