4

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

5 Answers 5

8
$('#buttonClear').click(function(e){
  // do whatever actions you need here
  e.preventDefault();
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, i had to change the actions to: $('#username').val(""); $('#password').val("");
3

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.

1 Comment

Thank you, i had to change the actions to: $('#username').val(""); $('#password').val("");
2

try e.preventDefault();

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

1 Comment

Thank you, i had to change the actions to: $('#username').val(""); $('#password').val("");
1

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" />

Comments

0

Why not just

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.