Ajax返回表格上方的数据

I am wondering how to join up a variable and to tell js where to place the returned data.

I need to change this line $("#commentbox").append(data); to $("#comment_'VAR PARENT DATA'").append(data); .There are a lot of forms on the page and I need the data to return directly above the form it was submitted from where there is a div called comment_(what ever the loop number is). There is a hidden input called parent in the Form which has the loop number.

Is there are a better way to do it?

$(function () {
    $('.reply').on('submit', function (e) {
        var parent = $('#parent').val();
        $.ajax({
            type: 'post',
            url: '/app/reply',
            data: $(this).serialize(),
            success: function (data) {
                $("#commentbox").append(data);
                $(".reply")[0].reset();
            }
        });
        e.preventDefault();
    });
});

Thanks

Use string concatenation to append the parent value to the ID prefix.

$(function () {
    $('.reply').on('submit', function (e) {
        var parent = $('#parent').val();
        $.ajax({
            type: 'post',
            url: '/app/reply',
            data: $(this).serialize(),
            success: function (data) {
                $("#comment_" + parent).append(data);
                $(".reply")[0].reset();
            }
        });
        e.preventDefault();
    });
});

Can you just do $("#comment_"+parent).append(data);

?

If I understood you, you want to have a dynamic selector. For that you could concatenate the "#comment_' string and the variable. Something like that '#comment_' + VAR_PARENT_DATA. It is difficult to anticipate what you are trying to achieve without more source code.