将每个模态关联到一个 id
Associated each modal to an id
我希望我的每个模态都有一个唯一的 ID。但我实际上在做的是给我:
Syntax error, unrecognized expression: #myModal(7)
下面是我的代码:
<tbody class="tbodyBookTable">
@foreach (var item in Model)
{
<tr class="trBookTable">
<th>@Html.DisplayFor(modelItem => item.Title)</th>
<th>@Html.DisplayFor(modelItem => item.Author)</th>
<th>@Html.DisplayFor(modelItem => item.DateRented)</th>
<th>@Html.DisplayFor(modelItem => item.ReturnDate)</th>
<th>
<button type="button" class="btn btn-default" data-toggle="modal" data-target="#myModal(@Html.DisplayFor(modelItem => item.BookID))">Yes</button>
<!-- Modal -->
<div class="modal fade" id="#myModal(@Html.DisplayFor(modelItem => item.BookID))" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Return confirmation</h4>
</div>
<div class="modal-body">
<p>Are you sure you want to return this book?.</p>
<p>Book title:@Html.DisplayFor(modelItem => item.Title)</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Yes</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</th>
</tr>
}
</tbody>
如您所见,我将 data-target
设置为
#myModal(@Html.DisplayFor(modelItem => item.BookID))
和我模型的id一样。我不明白为什么会收到此错误。有什么帮助吗?
您没有构建正确的 id
。使用这些:
<div id='myModal(' + @Html.DisplayFor(item => item.BookID) + ')'></div>
因为#
可能会产生冲突,所以不要在元素id中使用。
从 id
声明中删除标签符号 (#):
<div id='myModal(' + @Html.DisplayFor(item => item.BookID) + ')'></div>
data-target
使用主题标签符号,因为它会直接放入 jQuery 中以查找元素。在写 HTML 元素的 id
时,不要写 #
符号。
我希望我的每个模态都有一个唯一的 ID。但我实际上在做的是给我:
Syntax error, unrecognized expression: #myModal(7)
下面是我的代码:
<tbody class="tbodyBookTable">
@foreach (var item in Model)
{
<tr class="trBookTable">
<th>@Html.DisplayFor(modelItem => item.Title)</th>
<th>@Html.DisplayFor(modelItem => item.Author)</th>
<th>@Html.DisplayFor(modelItem => item.DateRented)</th>
<th>@Html.DisplayFor(modelItem => item.ReturnDate)</th>
<th>
<button type="button" class="btn btn-default" data-toggle="modal" data-target="#myModal(@Html.DisplayFor(modelItem => item.BookID))">Yes</button>
<!-- Modal -->
<div class="modal fade" id="#myModal(@Html.DisplayFor(modelItem => item.BookID))" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Return confirmation</h4>
</div>
<div class="modal-body">
<p>Are you sure you want to return this book?.</p>
<p>Book title:@Html.DisplayFor(modelItem => item.Title)</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Yes</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</th>
</tr>
}
</tbody>
如您所见,我将 data-target
设置为
#myModal(@Html.DisplayFor(modelItem => item.BookID))
和我模型的id一样。我不明白为什么会收到此错误。有什么帮助吗?
您没有构建正确的 id
。使用这些:
<div id='myModal(' + @Html.DisplayFor(item => item.BookID) + ')'></div>
因为#
可能会产生冲突,所以不要在元素id中使用。
从 id
声明中删除标签符号 (#):
<div id='myModal(' + @Html.DisplayFor(item => item.BookID) + ')'></div>
data-target
使用主题标签符号,因为它会直接放入 jQuery 中以查找元素。在写 HTML 元素的 id
时,不要写 #
符号。