How can I attach a click event to a button and prevent postback?
如何将单击事件附加到按钮并防止回发?
The code I have doesn't seem to be working.
我的代码似乎不能工作。
$('#btnNext').click(function() {
return false;
});
4 个解决方案
#1
56
$('#btnNext').click(function(e) {
e.preventDefault();
});
#2
16
I was working with a button and discovered that if you want to prevent the button from doing an auto-postback, you need to specify the type as "button". For example:
我使用了一个按钮,发现如果要阻止按钮进行自动回发,需要将类型指定为“button”。例如:
<button id="saveButton">Save</button>
--this will generate an auto-postback (tested in IE)
——这将生成一个自动回发(在IE中测试)
<button type="button" id="saveButton">Save</button>
--this will not
——这不会
Hope this helps someone.
希望这可以帮助别人。
#3
3
In Asp.net to prevent page postback on button click use preventDefault() function
在Asp.net中,为了防止页面回发,单击“使用preventDefault()”函数
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>WebForm1</title>
<script src="js/jquery-2.2.2.js" type="text/javascript"></script>
<script>
$(document).ready(function () {
$("#btnshow").click(function (e) {
e.preventDefault();
$("#Label1").show();
});
$("#btnhide").click(function (e) {
e.preventDefault();
$("#Label1").hide();
})
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Button ID="btnshow" runat="server" Text="Show" />
<asp:Button ID="btnhide" runat="server" Text="Hide" />
</form>
</body>
</html>
#4
0
inside button tag, use type="Button" to prevent from post back.
在按钮标签内部,使用type=" button "防止post返回。
#1
56
$('#btnNext').click(function(e) {
e.preventDefault();
});
#2
16
I was working with a button and discovered that if you want to prevent the button from doing an auto-postback, you need to specify the type as "button". For example:
我使用了一个按钮,发现如果要阻止按钮进行自动回发,需要将类型指定为“button”。例如:
<button id="saveButton">Save</button>
--this will generate an auto-postback (tested in IE)
——这将生成一个自动回发(在IE中测试)
<button type="button" id="saveButton">Save</button>
--this will not
——这不会
Hope this helps someone.
希望这可以帮助别人。
#3
3
In Asp.net to prevent page postback on button click use preventDefault() function
在Asp.net中,为了防止页面回发,单击“使用preventDefault()”函数
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>WebForm1</title>
<script src="js/jquery-2.2.2.js" type="text/javascript"></script>
<script>
$(document).ready(function () {
$("#btnshow").click(function (e) {
e.preventDefault();
$("#Label1").show();
});
$("#btnhide").click(function (e) {
e.preventDefault();
$("#Label1").hide();
})
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Button ID="btnshow" runat="server" Text="Show" />
<asp:Button ID="btnhide" runat="server" Text="Hide" />
</form>
</body>
</html>
#4
0
inside button tag, use type="Button" to prevent from post back.
在按钮标签内部,使用type=" button "防止post返回。