There is a code:

<form id="myForm" method="post"> <p><textarea id=f13 name="field1"></textarea></p> <p><input type="submit" value="Отправить"></p> </form> <div id="content">111</div> <div id="result">555</div> $(document).ready(function(){ $('#myForm').submit(function(){ var str = $(this).serialize(); var str2 = $('#f13').val(); str2.split('\n'); srt2.forEach(function(item, i, srt2) { $('.result').html(item); }); $.ajax({ type: "POST", url: "test.php", data: str, success: function(html){ $("#content").html(html); } }); return false; }); }); 

And then there is an error ... It is necessary that an array be created, the separator for which will be a new line, and then the result is output in turn.

  • one
    Just str2.split('\n'); - Sergey Gornostaev
  • Apparently I am Krivorukov and blind, but it didn’t work for me .... - Dvashington
  • one
    @Dvashington string var str3 = str2.split('\n'); replace with str2.split('\n'); - stackanon
  • one
    The logic of the code given by you is mysterious. In particular, the srt3.forEach(function(item, i, srt3) { $('#result').html(str3); }); construction srt3.forEach(function(item, i, srt3) { $('#result').html(str3); }); . Why for each element of the array in #result output the entire array? - Sergey Gornostaev
  • one
    Fundamentally nothing will change, meaningful #result will be overwritten as many times as there are elements in the array. Explain what you are trying to achieve. - Sergey Gornostaev

1 answer 1

 var result = $('#result'); $('#f13').keypress(function(e) { //Будем реагировать на ввод новой строки var code = (e.keyCode ? e.keyCode : e.which); if(code == 13) { var text = $(this).val(); var items = text.split('\n').map(function(item) { //Создаём массив, содержащий div для каждого строки return $('<div class="result">' + item + '</div>'); }); result.html(items); //Заменяем содержимое #result } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <textarea id="f13"></textarea> <div id="result"></div> 

  • I have everything easier. After clicking on submit, divas appear) - Swashington
  • one
    No problem, use the submit event. My code is just an example so that you understand the principle. - Sergey Gornostaev
  • So do. In the original, then I will feed each of the values ​​php file and produce a result from it. - Dvashington
  • one
    If you get an answer to your question, mark it with a solution. - Sergey Gornostaev
  • Rewrote under itself, everything turned out. I mark - Dvashington