บทความ > Javascript

Javascript : Disable enter submit form (Except shift Enter for textarea) Turn Back

เมื่อ 2016-11-23 11:46:51

 
This is the jquery code for disabling form submit by press Enter button.
But it allowed to key press Shift+Enter for insert new line to textarea
 
<script>
 
$(function() { // DOM ready
 
    $(window).keydown(function(event){
        if(event.which==13 && !$(event.target).is("textarea")) {
            event.preventDefault();
            return false;
        }
    });
 
});
 
</script>
 
Additionnal: (Force enable enter for textarea)
$('textarea').keypress(function(event) {
   if (event.which == 13) {
      event.preventDefault();
      var s = $(this).val();
      $(this).val(s+" ");
   }
});​
 
Thank and enjoy coding.
Turn Back

Automotive ดูทั้งหมด

IT Update ดูทั้งหมด