admin管理员组文章数量:1297127
Here is my HTML code
<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. 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? I tried to prevent the post back using jQuery
$(document).ready(function () {
$('#btn').click(function (evt) {
evt.preventDefault();
});
});
But this makes the required
validation not to fire.
Note: There are more than one button in the form.
Here is my HTML code
<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. 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? I tried to prevent the post back using jQuery
$(document).ready(function () {
$('#btn').click(function (evt) {
evt.preventDefault();
});
});
But this makes the required
validation not to fire.
Note: There are more than one button in the form.
Share Improve this question edited Jun 18, 2013 at 9:14 Felix Kling 817k181 gold badges1.1k silver badges1.2k bronze badges asked Jun 18, 2013 at 8:59 iJadeiJade 23.8k58 gold badges160 silver badges250 bronze badges 1- 1 Please don't use "leetspeak". There is no character limit for questions. It's "are" not "r". – Felix Kling Commented Jun 18, 2013 at 9:15
2 Answers
Reset to default 8change "click" event to "submit", and bind it not to btn but to form
$(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).
HTML:
<form id="form1" method="get" action="http://example.">
<input id="q" required />
<input id="w" />
<input id="btn" type="button" value="Cancel" />
<input id="btn" type="submit" value="Submit" />
Javascript
$('#form1').on("submit", function (evt) {
evt.preventDefault();
});
If that doesn't answer your question, please elaborate
本文标签: prevent postback using jqueryJavaScriptStack Overflow
版权声明:本文标题:prevent postback using jquery,javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741636597a2389674.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论