简体   繁体   中英

The data in the array does not change after the remove

Basically my code works like that:

1* Send the first line. 2* Get the response and sent it back. 3* remove one line and check the next line which will be the first line.

However in my code the data does not change at all in the whole process it checks only the first line again and again this is the code that i tried:

var resource = $('#resource').val().replace(/[\n\r](?!\w)/gi, "").split("\n");  

$.each(resource , function() {
  $.ajax({
    type:'POST',
    url :'curl_check.php',
    data: 'email='+resource[0],
    success: function(data) {
      alert(resource);
      resource.splice(0, 1);
      $('#resource').val(resource.join("\n"))
    }   
  });
});

Basically even the line is removed from the text-area the values still the same.

The code is weird, I think you hit issue because you 'strip' the resource array in the success callback which is async, that means you are always sending the first entry of the array in the loop...

A 'hack' that you can try to proof this is to add async: false to the ajax parameter, although that's not quite the right way to really do this..

To me, it looks like you're stripping out the the newlines, and line-feed characters with your regex on line 1, but then you are attempting to perform a split(\\n). This split will fail because you have just removed the newlines with nothing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM