【问题标题】:How to use pass ajax data that is displaying in html?如何使用在 html 中显示的传递 ajax 数据?
【发布时间】:2018-07-31 01:32:06
【问题描述】:

我有一个问题,我似乎无法为我的问题找到“最佳”解决方案。我有一个通过 jQuery 在 DOM 中显示数据的 AJAX 调用。

JSON

[{
    "id":"123"
},
{   "id":"456"
}]

JS

$(document).ready(function() {
    $.ajax({
        url: 'get/data',
        dataType: 'json',
        data: '{}',
        success: function(data) {
            var html = '';
            $.each(data, function(i) {
               html += '<p>My ID: ' + data[i].id + '</p>';
            }
            $('#id').append(html);
        },
        error: function(err) {
            console.log(err);
        }
    })
});

如您所见,我在此处显示该数据。我想通过选择复选框并按下按钮或单击链接将该 ID 传递给另一个 AJAX 调用。实现这一目标的最佳方法是什么?

编辑:我很抱歉,我的实际问题与数组数据有关。我已经更新了代码。我现在在 html 中显示一个 JSON 数组,我想传递我单击的用户/行的 id?

谢谢, 安迪

【问题讨论】:

  • 因此将您的 id 设置为一个空的全局变量,并在第一次 ajax 成功时为其赋值。在您的第二个 dom 事件(如复选框/按钮)上单击调用第二个 ajax 并传递该值。

标签: javascript jquery json ajax rest


【解决方案1】:
$(document).ready(function() {
var resid;
     $.ajax({
           url: 'get/data',
           dataType: 'json',
           data: '{}',
           success: function(data) {
                resid = data.id;
                var html = '';
                html += '<p>My ID: ' + data.id + '</p>'; $('#id').append(html);
           },
           error: function(err){
               console.log(err); 
           }

      });

     $('link').click(function () {
       // pass id to second ajax
     });

});

【讨论】:

  • 感谢您的回复,我很抱歉,但最初的问题并没有完全问我在寻找什么。你能检查更新的问题吗?
【解决方案2】:

试试 jquery 模板 https://github.com/codepb/jquery-template 希望能帮到你

【讨论】:

    猜你喜欢
    • 2015-10-27
    • 2013-12-30
    • 2019-09-15
    • 2015-02-23
    • 1970-01-01
    • 2014-06-11
    • 1970-01-01
    • 2021-06-18
    • 2014-07-02
    相关资源
    最近更新 更多