A
A
Anon33632020-05-24 00:51:48
AJAX
Anon3363, 2020-05-24 00:51:48

Why does the ajax method of collecting a value using each (took the value of several inputs) accept only one?

$('.send').click(function(){
    let val;
    let name;
    $('.answer').each(function(item){
        val = $(this).val()
        name = $(this).attr('name')
        // console.log(val)
    })
    $.ajax({
        type:"post",
        url:"server.php",
        data:{action:"test",value:val,name:name},
        success:function(r){
            r = JSON.parse(r)
            console.log(r)
        }
    })
})

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
scottparker, 2020-05-24
@scottparker

so every time you overwrite the values ​​of the variables val and name in each, it is logical that only the last ones will remain. save to an array or something) and there is also a serialization of the form, this is a function, similar to what you tried to do

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question