Jquery:按下输入类型=提交时防止重新加载页面
我在 MVC 3 (Razor) 项目中有以下代码:
<div class="user_info" id="id_user_info">
<script type="text/ecmascript" >
$(function () {
$('#buttonClear').live('submit', function (e) { return false; });
$('#buttonClear').bind('click', function () {
$('username').value = "";
$('password').value = "";
});
</script>
@using (Html.BeginForm("Login", "Account", FormMethod.Post, new { id = "loginForm" }))
{
<label for="username">Enter:</label>
<input name="username" id="username" type="text" value="" /><br />
<input name="password" id="password" type="password" value="" /><br />
<input name="rememberMe" id="rememberMe" type="checkbox" value="true" />
<label for="rememberMe">Remember me?</label><br />
<input id="buttonLogin" type='submit' value="Login" />
<input id="buttonClear" type='submit' value="Clear" />
}
</div>
由于样式原因,我需要将“buttonClear”保留为类型 sumbit,但我需要禁用/避免/防止它在按下后重新加载页面。
有没有办法从“buttonClear”中删除“提交事件”?
谢谢 里卡多
I have the following code in a MVC 3 (Razor) project:
<div class="user_info" id="id_user_info">
<script type="text/ecmascript" >
$(function () {
$('#buttonClear').live('submit', function (e) { return false; });
$('#buttonClear').bind('click', function () {
$('username').value = "";
$('password').value = "";
});
</script>
@using (Html.BeginForm("Login", "Account", FormMethod.Post, new { id = "loginForm" }))
{
<label for="username">Enter:</label>
<input name="username" id="username" type="text" value="" /><br />
<input name="password" id="password" type="password" value="" /><br />
<input name="rememberMe" id="rememberMe" type="checkbox" value="true" />
<label for="rememberMe">Remember me?</label><br />
<input id="buttonLogin" type='submit' value="Login" />
<input id="buttonClear" type='submit' value="Clear" />
}
</div>
I need to keep the "buttonClear as type sumbit because of the style, but i need to disable/avoid/prevent it from reloading the page once pressed.
Is there a way to remove the "submit event" from "buttonClear"?
Thanks
Ricardo
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我建议删除您绑定到
submit
事件的处理程序,并在click
事件处理程序中使用preventDefault
:您尝试过的 commit 事件方法将不起作用,因为
submit
事件只能绑定到form
元素,而不能绑定到提交表单的按钮。I would suggest removing the handler you're binding to the
submit
event, and instead usingpreventDefault
in theclick
event handler:The
submit
event approach you have tried will not work becausesubmit
events can only be bound toform
elements, not the buttons that submit the form.尝试
e.preventDefault();
try
e.preventDefault();
虽然我意识到这篇文章有点旧,但我也想提供这个答案以供记录。
通常,一张表单不会有多个提交按钮。此外,由于您实际上并不需要提交功能,因此使用此类型并不准确。最后,您正在寻找“清除”或“重置”功能。你的按钮应该是:
While I realize this post is a little older, I wanted to provide this answer for the record as well.
Normally you would not have multiple submit buttons for one form. Additionally, since you don't actually want submit functionality it wouldn't be accurate to use this type. Finally you are looking for a "Clear" or "Reset" functionality. Your button should be:
为什么不只是
Why not just