Asp.net 为什么alert()调用会导致我的事件再次触发?
我正在努力完成。我来到了为复选框添加事件处理程序的部分。我在这方面遇到了一些问题,所以我加入了alert()调用,以便能够看到脚本正在执行。我还调用了控制器中的Console.WriteLine(),以报告框何时成功处理以及何时失败 当我在markCompleted()函数中调用alert()时,消息会出现两次。在运行项目的控制台窗口中,我首先看到成功报告,然后是失败报告。当存在alert()调用时,复选框的click事件似乎被触发两次。为什么呢?当我删除alert()调用时,我在控制台窗口中只收到一条消息,并且操作成功 以下是教程告诉我要加入的JavaScript:Asp.net 为什么alert()调用会导致我的事件再次触发?,asp.net,asp.net-core,asp.net-mvc-4,Asp.net,Asp.net Core,Asp.net Mvc 4,我正在努力完成。我来到了为复选框添加事件处理程序的部分。我在这方面遇到了一些问题,所以我加入了alert()调用,以便能够看到脚本正在执行。我还调用了控制器中的Console.WriteLine(),以报告框何时成功处理以及何时失败 当我在markCompleted()函数中调用alert()时,消息会出现两次。在运行项目的控制台窗口中,我首先看到成功报告,然后是失败报告。当存在alert()调用时,复选框的click事件似乎被触发两次。为什么呢?当我删除alert()调用时,我在控制台窗口中只
$(document).ready(function () {
// Wire up all of the checkboxes to run markComnpleted()
//alert("Document ready function");
$('.done-checkbox').on('click', function (e) {
markCompleted(e.target);
});
});
function markCompleted(checkbox) {
alert("Mark completed function.");
checkbox.disabled = true;
var row = checkbox.closest('tr');
$(row).addClass('done');
var form = checkbox.closest('form');
form.submit();
}
这是我的控制器代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using AspNetCoreTodo.Services;
using AspNetCoreTodo.Models;
namespace AspNetCoreTodo.Controllers
{
public class ToDoController : Controller
{
private readonly IToDoItemService _toDoItemService;
public ToDoController(IToDoItemService toDoItemService)
{
_toDoItemService = toDoItemService;
}
public async Task<IActionResult> Index()
{
// Get to-do items from database
var items = await _toDoItemService.GetIncompleteItemsAsync();
// Put items into a model
var model = new ToDoViewModel()
{
Items = items
};
// Render view using the model
return View(model);
}
[ValidateAntiForgeryToken]
public async Task<IActionResult> AddItem(ToDoItem newItem)
{
if (!ModelState.IsValid)
{
return RedirectToAction("Index");
}
var successful = await _toDoItemService.AddItemAsync(newItem);
if (!successful)
{
return BadRequest("Could not add item.");
}
return RedirectToAction("Index");
}
[ValidateAntiForgeryToken]
public async Task<IActionResult> MarkDone(Guid id)
{
if (id == Guid.Empty)
{
Console.WriteLine("Received invalid or empty Guid");
return RedirectToAction("Index");
}
bool successful = await _toDoItemService.MarkDoneAsync(id);
if (!successful)
{
Console.WriteLine("Failed to mark item as done.");
return BadRequest("Could not mark item as done.");
}
Console.WriteLine("Item marked done.");
return RedirectToAction("Index");
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Threading.Tasks;
使用Microsoft.AspNetCore.Mvc;
使用AspNetCoreTodo.Services;
使用AspNetCoreTodo.Models;
命名空间AspNetCoreTodo.Controllers
{
公共类ToDoController:控制器
{
私有只读iTooItemService(toDoItemService);
公共ToDooController(iToItemService toDoItemService)
{
_toDoItemService=toDoItemService;
}
公共异步任务索引()
{
//从数据库获取待办事项
var items=wait_toDoItemService.GetIncompleteItemsAsync();
//将项目放入模型中
var model=new-ToDoViewModel()
{
项目=项目
};
//使用模型渲染视图
返回视图(模型);
}
[ValidateAntiForgeryToken]
公共异步任务附加项(ToDoItem newItem)
{
如果(!ModelState.IsValid)
{
返回操作(“索引”);
}
var successful=wait _toDoItemService.additemsync(newItem);
如果(!成功)
{
返回错误请求(“无法添加项目”);
}
返回操作(“索引”);
}
[ValidateAntiForgeryToken]
公共异步任务MarkDone(Guid id)
{
if(id==Guid.Empty)
{
Console.WriteLine(“接收到无效或空Guid”);
返回操作(“索引”);
}
bool successful=wait_toDoItemService.MarkDoneAsync(id);
如果(!成功)
{
Console.WriteLine(“未能将项目标记为完成”);
返回错误请求(“无法将项目标记为已完成”);
}
Console.WriteLine(“标记为完成的项目”);
返回操作(“索引”);
}
}
}
最后,以下是服务的代码:
public async Task<bool> MarkDoneAsync(Guid id)
{
var item = await _context.Items
.Where(x => x.Id == id)
.SingleOrDefaultAsync();
if (item == null)
return false;
item.IsDone = true;
int saveResult = await _context.SaveChangesAsync();
return saveResult == 1;
}
公共异步任务MarkDoneAsync(Guid id)
{
var item=await\u context.Items
.其中(x=>x.Id==Id)
.SingleOrDefaultAsync();
如果(项==null)
返回false;
item.IsDone=true;
int saveResult=wait_context.SaveChangesAsync();
返回saveResult==1;
}
可能存在以下两个问题之一:
1.您已经包含了min.js和js,因此它将绑定事件两次。若要排除这种情况,请在添加前单击“取消绑定”,在这种情况下,应执行一次。
2.如果这没有帮助,您的事件可能正在传播到父元素,您希望在调用markcompleted之前尝试e.PreventDefault(),以排除此问题。可能存在以下两个问题之一: 1.您已经包含了min.js和js,因此它将绑定事件两次。若要排除这种情况,请在添加前单击“取消绑定”,在这种情况下,应执行一次。 2.如果这没有帮助,您的事件可能正在传播到父元素,您希望在调用markcompleted以排除此事件之前尝试e.PreventDefault()