2

input name 'btnSaveScore' is clicked When keypress ENTER in TextBox.

I do not want to call 'btnSaveScore' when keypress ENTER in TextBox.

I guess that 'btnSaveScore' is a default button.

Can I set default button = false ?

//Code

 @using (Html.BeginForm("ScoreStudent", "Score", FormMethod.Post))
{
  ...
  ...
  ...
 <input name ="btnSaveScore" id="btnSaveScore" value="submit score" type="submit" /> 
}
//End Code

Help me please, Thank you.

pompom
  • 109
  • 1
  • 5
  • possible duplicate of [Prevent Users from submitting form by hitting enter](http://stackoverflow.com/questions/895171/prevent-users-from-submitting-form-by-hitting-enter) – Raphaël Althaus Aug 19 '13 at 09:18

4 Answers4

1

This isn't an MVC problem - this is a HTML issue. If your button isn't supposed to submit the form then don't use type="submit" use type="button" instead.

Alternatively you can solve it using JS

$("#form").keypress(function(e){
    return e.which != 13;
}
James
  • 80,725
  • 18
  • 167
  • 237
1

Add onkeydown attribute in your textbox in following way:

onkeydown="if (event.keyCode == 13) return false;"

Your textbox should be like this:

<input type="text" id="textboxId" onkeydown="if (event.keyCode == 13) return false;" />
Akash KC
  • 16,057
  • 6
  • 39
  • 59
0

Try this

<input name ="btnSaveScore" id="btnSaveScore" value="submit score" type="submit" /> 

to

 <input name ="btnSaveScore" id="btnSaveScore" value="submit score" type="button" /> 
Amit
  • 15,217
  • 8
  • 46
  • 68
0

Thank you everyone. Code below is work for me

$('#textboxname').live("keypress", function (e) 
{ 
  if (e.keyCode == 13) 
  { 
       // do something 

       e.preventDefault(); 
       return false; 
  } 
}); 

and

@using (Html.BeginForm("ScoreStudent", "Score", FormMethod.Post)) 
{ 
 ... 
 ... 
 <input name ="btnSaveScore" id="btnSaveScore" value="submit score" type="submit" /> 
}
pompom
  • 109
  • 1
  • 5