【问题标题】:Onchange Event for Text Area文本区域的 Onchange 事件
【发布时间】:2015-09-11 12:32:53
【问题描述】:

我一直在尝试在文本区域上使用和 onchange 事件,但是没有任何效果。正常的 change() 或 onChange() 函数什么都不做。

我已经尝试过

$('#areaName').on('input', function(){})
$('#areaName').bind('input', function(){})

我已经尝试了我在网上找到的所有解决方案,但无济于事。

当文本区域中的文本无论如何都被修改时,我只需要这段代码来触发一个函数。

【问题讨论】:

  • keypress 活动怎么样?

标签: javascript events textarea onchange


【解决方案1】:

使用键盘事件,plunker

js:

$(document).ready(function(){
  $("#area").on("keypress",function(){
    alert("hello")
  })

})

如果您使用 jqueryui,您可以将 resize 事件捕获为:

 $("#area").resizable({
    resize: function() {
        alert("hello2")
    }
 });

【讨论】:

  • 那是 jquery,不是 javascript
【解决方案2】:

试试这个:

 <!DOCTYPE html>

    <head>
      <meta charset="utf-8">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
      <script src="//cdnjs.cloudflare.com/ajax/libs/select2/4.0.0/js/select2.min.js"></script>
      <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.12.0/jquery.validate.min.js" type="text/javascript"></script>
      <script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
      <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
      <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
    <style>

    </style>
    </head>
    <body>

     <textarea rows="4" cols="50" id="textArea">

     </textarea>
    <script>
      $(document).ready(function(){
        $('#textArea').bind('input propertychange', function() {
          console.log(this.value);
        });
      });
    </script>
    </body>
    </html>

【讨论】:

    【解决方案3】:

    以下代码按预期工作,在 Firefox 中测试

       $(function ()
        {
            $('#areaName').change(function () {
                alert($('#areaName').val());
            });
        });
    

    【讨论】:

      【解决方案4】:

      这在 Firefox 中适用于我:

       <textarea oninput="alert('input detected')";>initial input text</textarea>
      

      【讨论】:

        猜你喜欢
        • 2011-02-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-31
        • 2017-10-16
        • 2011-09-04
        • 1970-01-01
        相关资源
        最近更新 更多