jQuery each line in textarea

后端 未结 4 1700
别跟我提以往
别跟我提以往 2021-02-20 12:17

html



jquery

相关标签:
4条回答
  • 2021-02-20 12:39

    I think you can't use the html tags in this manner for this you have to specify and ID for each tag and then access in the jQuery function.

     <textarea id="gps" name="gps"></textarea>
    <button id="btn">Click</button>
    $('#btn').click(function(){
        var arrayOfLines = $('#gps').val().split('\n');
        $.each(arrayOfLines, function(index, item) {
            $this = $(this);
            console.log($this);         
        });
    });
    
    0 讨论(0)
  • 2021-02-20 12:52

    Inside .each loop line id 'item' object, not 'this'.

    <textarea id="gps" name="gps"></textarea>
      <button id="btn">Click</button>
      $('#btn').click(function(){
          var arrayOfLines = $('#gps').val().split('\n');
          $.each(arrayOfLines, function(index, item) {
              console.log('here is line:', item);         
          });
      });
    
    0 讨论(0)
  • 2021-02-20 12:53

    You are placing a string into a jQuery object. Just use the item instead:

    $('button').click(function(){
        var arrayOfLines = $('#gps').val().split('\n');
        $.each(arrayOfLines, function(index, item) {
            console.log(item);
        });
    });
    
    0 讨论(0)
  • 2021-02-20 12:53

    You're not dealing with "this" properly. Try the following:

    $('button').click(function(){
      var arrayOfLines = $('#gps').val().split('\n');
      $.each(arrayOfLines, function(index, item) {
        console.log(this);
      });
    });
    

    Note that the "this" variable in the inner function starts with the newline, I believe. But this should get you on the right track.

    0 讨论(0)
提交回复
热议问题