Javascript how to split newline

后端 未结 12 1922
一整个雨季
一整个雨季 2020-11-28 06:48

I\'m using jquery, and I have a textarea. When I submit by my button I will alert each text separated by newline. How to split my text when there is a newline?



        
12条回答
  •  一生所求
    2020-11-28 07:30

    1. Move the var ks = $('#keywords').val().split("\n"); inside the event handler
    2. Use alert(ks[k]) instead of alert(k)

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

    Demo

提交回复
热议问题