[英]prevent postback using jquery,javascript
Here is my HTML code 这是我的HTML代码
<form id="form1" runat="server">
<input id="q" required />
<input id="btn" type="submit" value="Search" />
</form>
I'm trying the HTML 5 required
feature in asp.net. 我在asp.net中尝试HTML 5
required
功能。 The above code works. 上面的代码有效。 But a post back also occurs.
但也会发生回帖。 Is there a way to prevent the post back using JavaScript, jQuery or any other method?
有没有办法使用JavaScript,jQuery或任何其他方法阻止回发? I tried to prevent the post back using jQuery
我试图使用jQuery阻止回发
$(document).ready(function () {
$('#btn').click(function (evt) {
evt.preventDefault();
});
});
But this makes the required
validation not to fire. 但这使得
required
验证不会触发。
Note: There are more than one button in the form. 注意:表单中有多个按钮。
change "click" event to "submit", and bind it not to btn but to form 将“click”事件更改为“submit”,并将其绑定到btn而不是btn
$(document).ready(function () {
$('#form1').on("submit", function (evt) {
evt.preventDefault();
});
});
Here is the updated JsFiddle which has two inputs (one is required) and two buttons (one is submit). 这是更新的JsFiddle ,它有两个输入(一个是必需的)和两个按钮(一个是提交)。
HTML: HTML:
<form id="form1" method="get" action="http://example.com">
<input id="q" required />
<input id="w" />
<input id="btn" type="button" value="Cancel" />
<input id="btn" type="submit" value="Submit" />
Javascript 使用Javascript
$('#form1').on("submit", function (evt) {
evt.preventDefault();
});
If that doesn't answer your question, please elaborate 如果这不能回答你的问题,请详细说明
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.