JQuery - 通过ajax发送输入(由ajax获取)

时间:2017-10-03 18:12:57

标签: javascript php jquery ajax input

我有一个应用程序,在执行搜索后,返回多个"字段集"一些隐藏的输入(通过AJAX!)。 我想使用这些输入通过AJAX将信息发送到服务器(再次)。 这些输入的名称会自动列出前缀:

" video_url_1"," video_url_2"等

当用户点击该按钮时," video_url_1"或" video_url_2"将通过AJAX发送,具体取决于它点击的按钮。为了解决这个问题,我得到了点击按钮的名称,然后我剪切了名称,这样我只有一个数字,这个数字我放入一个变量,然后在"数据"中使用它。 AJAX部分。

我通过发送本地存储的输入进行了测试,但是当它尝试发送以前由ajax获得的输入时,它不起作用。 有什么不对?这是我的代码:

        $(document).ajaxComplete(function(){
            $('a.report_video').click(function() { 
                var idbutton = $(this).attr('id');
                var idreport = idbutton.replace('report_video_', '');
                //I'm still not using these variables, can they be used to pass the input data to ajax?
                var videourl = $("#video_url_" + idreport).val();
                var videoid = $("#video_id_" + idreport).val();
                var videoserver = $("#server").val();
                ///////////

                $.ajax({
                    type : 'POST',
                    url  : 'https://example.com/script/script.php',
                    data : $($("#video_url_" + idreport)).serialize(), //It doesn't work
                    //For example, data: $("#server").serialize() 
                    //Work fine, this input is stored locally. 
                    beforeSend: function(){
                        $('#video_report_' + idreport).html('<img src="'+pluginUrl+'./assets/img/loading.svg" />');
                    }
                }).done(function(data) {
                    $('#video_report_' + idreport).html(data);
                });

                return false; 
            });     


        });

编辑: 我刚刚按照Kevin B的建议进行了一些测试,我发现我遇到的问题是在尝试通过Ajax发送两个动态ID时的语法。 问题是我不知道如何正确地编写它们,我知道这是问题所在,因为当我尝试单独发送它们时它们确实有效...

data : $($("#video_id_" + idreport), $("#video_url_" + idreport)).serialize(),

1 个答案:

答案 0 :(得分:-1)

我不确定我是否完全理解你的问题,但这可能有所帮助。

您可以在第一次AJAX来电的.success()方法中拨打第二个AJAX来电。基本上链接响应。

$('#btn').click(function() {
    $.ajax({
        type: "POST",
        url: 'someURL',
        data: someData
    }).done(function(firstCallData) {
        // This OPTIONAL method fires when the AJAC call succeeded
        // You can also put another AJAX call in here with the data returned from the first call
        $.ajax({
            type: "POST",
            url: 'someURL',
            data: firstCallData
        }).done(function(data) {
            // Do something with second AJAX call with data
        }).fail(function(data) {
            // Second AJAX call failed, handle error
        });
    }).fail(function(data) {
        // This OPTIONAL method fires when the first response failed
    }).always(function(data) {
        // This OPTIONAL method fires regardless if the first call succeeded or failed.
    });
});