如何防止on.submit事件更改/重新加载页面?
问题描述:
在 Javascript 中,当我提供 onSubmit
函数时,我从函数返回'false',它阻止了页面更改/
In Javascript when I provide an onSubmit
function, and I return 'false' from the function, it prevents the page from changing/reloading.
但是在 Dart 中, onSubmit.listen(...)
不接受退货类型。
However in Dart the onSubmit.listen(...)
function does not take a return type.
如何阻止提交发送表单数据并更改/重新加载网页?
答
在 on.submit.add(...)
回调中,接收 Event
作为参数。 Event
对象提供了一个 preventDefault()
方法以防止或取消默认操作。
Within the on.submit.add(...)
callback, you will receive the Event
as an argument. The Event
object provides a preventDefault()
method to prevent or cancel the default action.
querySelector('#myForm').onSubmit.listen((Event e) {
// ... your code here
e.preventDefault();
});