jquery - 执行另一个 ajax 请求后 Ajax 请求不起作用
问题描述
我有一个主视图,其中有 2 个表。使用 ajax,我调用了一个模态文本框窗口。我填写它们并单击添加。当模态窗口不是通过 ajax 而是简单地通过具有必要的 html 参数的按钮调用时,通常会添加记录,并且在 html.partial 的帮助下在主窗体上调用局部视图本身。我重新调用了 ajax 请求,添加的 ajax 按钮请求已停止工作。我不明白这是怎么回事,请告诉我出了什么问题。这是我的代码。主要观点:
@{
ViewBag.Title = "Index";
}
@model IEnumerable<AjaxTest.Models.Book>
<h2>Каталог книг</h2>
<input id="modRender" type="submit" value="Добавить" />
<body>
<div id="result"></div>
<div>
<table id="tab" class="table table-bordered">
<thead>
<tr>
<th>Название</th>
<th>Автор</th>
<th>Цена</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(i => item.Name)
</td>
<td>
@Html.DisplayFor(i => item.Author)
</td>
<td>
@Html.DisplayFor(i => item.Price)
</td>
</tr>
}
</tbody>
</table>
</div>
</body>
<h2>Список пользователей</h2>
<body>
<div>
<table class="table table-bordered">
<thead>
<tr>
<th>Имя</th>
<th>Фамилия</th>
<th>Возраст</th>
</tr>
</thead>
<tbody>
@foreach (var item in ViewBag.Users as List<AjaxTest.Models.User>)
{
<tr>
<td>
@Html.DisplayFor(i => item.Name)
</td>
<td>
@Html.DisplayFor(i => item.Surname)
</td>
<td>
@Html.DisplayFor(i => item.Age)
</td>
</tr>
}
</tbody>
</table>
</div>
</body>
<div id="result"></div>
@section scripts{
<script type="text/javascript">
$('#modRender').on('click', function () {
$.ajax({
type: "POST",
url: "/Home/ModalCreate",
success: function (data) {
$('#result').html(data);
$('#mod').modal('show');
}
});
});
$('#btnAdd').on('click', function () {
$.ajax({
type: "POST",
url: "/Home/Create",
data: {
"Name": $('#txtName').val(),
"Author": $('#txtAuthor').val(),
"Price": $('#txtPrice').val()
},
success: function (data) {
$('#tab tbody').append(data);
}
});
});
$("#result").on('hidden.bs.modal', function () {
$('#mod').remove();
});
</script>
}
查看模态窗口:
<div id="mod" class="modal fade" >
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h4 class="modal-title">Новая книга</h4>
</div>
<div class="modal-body">
<table>
<tr>
<td><p>Название</p></td>
<td><input type="text" id="txtName" /></td>
</tr>
<tr>
<td><p>Автор</p></td>
<td><input type="text" id="txtAuthor" /></td>
</tr>
<tr>
<td><p>Цена</p></td>
<td><input type="text" id="txtPrice" /></td>
</tr>
</table>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Закрыть</button>
<button id="btnAdd" type="submit" data-dismiss="modal" class="btn btn-primary">Добавить</button>
</div>
</div>
</div>
</div>
控制器:
Context db = new Context();
[HttpGet]
public ActionResult Index()
{
List<Book> books = db.Books.ToList();
List<User> users = db.Users.ToList();
ViewBag.Users = users;
return View(books);
}
[HttpPost]
public ActionResult Create(Book book)
{
db.Books.Add(book);
db.SaveChanges();
return PartialView(book);
}
[HttpPost]
public ActionResult ModalCreate()
{
return PartialView();
}
解决方案
推荐阅读
- graphql - Apollo Mock Error state -> Error: No more mocked response for the query
- javascript - 反应上下文不会导致重新渲染
- java - 使用注入点测试自定义验证器,WeldUnit 没有注入休眠 bean 验证工厂
- amazon-web-services - EC2 实例中的 API 成功使用端口 5000,但是当从负载均衡器到达时,端口不工作
- python - Windows、PyCharm:DPI-1047:找不到 32 位 Oracle 客户端库
- sql - 使用 GROUP BY 后查找具有 MAX 值的行
- javascript - 无法从 module.exports 得到我想要的
- audio - FFMPEG:连接视频的标题卡导致慢动作
- azure-devops - Azure DevOps 管道 - 如何在使用多个 Git 源存储库时标记源
- google-cloud-platform - 是否可以在谷歌云上持续训练 AutoML 自然语言模型以自动提高性能?