Javascript如何拆分换行符

问题描述:

我使用的是jquery,而且我有一个textarea,所以当我提交按钮时,我会用换行符分隔每个文本。如何从换行符拆分?

I'm use jquery, and I have a textarea, so when I submit button I will alert every text separate by newline. How to split from newline?

  var ks = $('#keywords').val().split("\n");
  (function($){
     $(document).ready(function(){
        $('#data').submit(function(e){
           e.preventDefault();
           alert(ks[0]);
           $.each(ks, function(k){
              alert(k);
           });
        });
     });
  })(jQuery);

示例输入:

Hello
There

我想要的结果是:

alert(Hello); and
alert(There)


尝试初始化提交函数中的 ks 变量。

Try initializing the ks variable inside your submit function.

  (function($){
     $(document).ready(function(){
        $('#data').submit(function(e){
           var ks = $('#keywords').val().split("\n");
           e.preventDefault();
           alert(ks[0]);
           $.each(ks, function(k){
              alert(k);
           });
        });
     });
  })(jQuery);