Disable New Line in Textarea when Pressed ENTER

后端 未结 4 1047
没有蜡笔的小新
没有蜡笔的小新 2020-12-01 18:06

I am calling a function whenever someone press enter in the textarea. Now I want to disable new line or break when ent

相关标签:
4条回答
  • 2020-12-01 18:23

    For Angular Users

    While there are existing working solutions, if anyone comes across this question using Angular, you can disable new lines with the following:

    Add <textarea ng-trim="false" ng-model='your.model' ...

    In your controller, add:

    $scope.$watch('your.model', function(newvalue, oldvalue) {
        if (newvalue && newvalue.indexOf('\n') > -1) {
           $scope.your.model = oldvalue;
        }
    });
    
    0 讨论(0)
  • 2020-12-01 18:28

    use the input tag instead of textArea tag in your HTML

    0 讨论(0)
  • 2020-12-01 18:30

    try this

    $("textarea").keydown(function(e){
    // Enter was pressed without shift key
    if (e.keyCode == 13 && !e.shiftKey)
    {
        // prevent default behavior
        e.preventDefault();
    }
    });
    

    update your fiddle to

    $(".Post_Description_Text").keydown(function(e){
    if (e.keyCode == 13 && !e.shiftKey)
    {
      // prevent default behavior
      e.preventDefault();
      //alert("ok");
      return false;
      }
    });
    
    0 讨论(0)
  • 2020-12-01 18:39
        $(".Post_Description_Text").keypress(function(event) {
    
          if (event.which == 13) {        
    
            alert("Function is Called on Enter");
    
              event.preventDefault(); //Add this line to your code
    
           }
    
       });
    
    0 讨论(0)
提交回复
热议问题