Jquery:按下输入类型=提交时防止重新加载页面

发布于 2024-11-24 16:40:20 字数 1169 浏览 0 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

给不了的爱 2024-12-01 16:40:20
$('#buttonClear').click(function(e){
  // do whatever actions you need here
  e.preventDefault();
});
$('#buttonClear').click(function(e){
  // do whatever actions you need here
  e.preventDefault();
});
娇纵 2024-12-01 16:40:20

我建议删除您绑定到 submit 事件的处理程序,并在 click 事件处理程序中使用 preventDefault

$('#buttonClear').bind('click', function (e) {
   e.preventDefault();
   $('username').value = "";
   $('password').value = "";
});

:您尝试过的 commit 事件方法将不起作用,因为 submit 事件只能绑定到 form 元素,而不能绑定到提交表单的按钮。

I would suggest removing the handler you're binding to the submit event, and instead using preventDefault in the click event handler:

$('#buttonClear').bind('click', function (e) {
   e.preventDefault();
   $('username').value = "";
   $('password').value = "";
});

The submit event approach you have tried will not work because submit events can only be bound to form elements, not the buttons that submit the form.

冷夜 2024-12-01 16:40:20

尝试e.preventDefault();

   $('#buttonClear').bind('click', function (e) {
       e.preventDefault();
       $('username').value = "";
       $('password').value = "";
    });

try e.preventDefault();

   $('#buttonClear').bind('click', function (e) {
       e.preventDefault();
       $('username').value = "";
       $('password').value = "";
    });
污味仙女 2024-12-01 16:40:20

虽然我意识到这篇文章有点旧,但我也想提供这个答案以供记录。

>

通常,一张表单不会有多个提交按钮。此外,由于您实际上并不需要提交功能,因此使用此类型并不准确。最后,您正在寻找“清除”或“重置”功能。你的按钮应该是:

<input id="buttonClear" type="reset" value="Clear" />

While I realize this post is a little older, I wanted to provide this answer for the record as well.

<input id="buttonClear" type='submit' value="Clear" />

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:

<input id="buttonClear" type="reset" value="Clear" />
一指流沙 2024-12-01 16:40:20

为什么不只是

$('#buttonClear').click(function() {
    return false;
});

Why not just

$('#buttonClear').click(function() {
    return false;
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文