weixin_33694620 2017-05-22 15:53 采纳率: 0%
浏览 38

Ajax中的多个动态URL

I'm having some difficulty here in importing up to 6 URLS into ajax. Essentially you take a form and enter into the form input field a URL. And then press 'load'. The jquery [see JS#1] will grab what was entered and load the Ajax call.

However if I press the 'add-more' button, it will add up to 5 more input fields. The problem here is when filling in the 5 more input fields and pressing 'load'
It doesn't load the ajax URLS separately. Just an empty array. I did some research into deferred js loading. But I'm obviously not doing something right.

I then tried a different approach [see JS#2]. Basically writing the ajax calls manually. And this gave me better results. However when I enter in three input fields, and press 'load' it then only grabs one of the input fields and loops that three times.

HTML

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <div id="tFormBox">
        <div class="tInputTop">
            <h1>Add Up to 6</h1>
        </div>
        <div class="tInputBottom">
            <label class="sr-only" for="cInput">Enter Name</label>
            <div class="c-input-fields">
                <div id="cInput">
                    <input class="form-control" name="cInput" placeholder="Enter Name">
                    <div class="add-more"></div>
                </div>
            </div><button id="chInputBtn">LOAD</button>
        </div>
    </div>
</body>
</html>

JS #1

var feed;
var sURLs = document.getElementsByClassName('cInput');
$('#cInput').click(function() {
    $(this).toggleClass('focused');
})
$('#cInput > .add-more').click(function() {
    // -- Limit to 6 more Additional Input Boxes --//
    var i = $('.InputBottom div').length;
    var cInput = $('<div id="cInput ' + i + ' "><input class="cInput" class="form-control" name="cInput" placeholder="Enter Name"></input></div>');
    $.each(cInput, function(i, val) {
        alert(cInput[i]);
    });
    if (i <= 7) {
        console.log('adding')
        $(cInput).appendTo('.c-input-fields');

    } else {
        console.log('maxed')
    }
})
$('#cInput').on('click', function(tags) {
    getS();
})

function getS() {
    // --grab URLS to a maximum of 6 --//
    for (var i = 0; i < sURLs.length; i++) {
        var sValue = sURLs[i].value;
    }

    function multipleAjax(number, deferredObject) {
        $.ajax({
            url: '/' + 'sValue',
            type: "GET",
            success: function(data) {
                results.push(data);
                deferredObject.resolve();
            }
        });
    }
    var deferreds = [];
    var results = [];
    var nothingImportant;
    for (var i = 0; i < nothingImportant; i++) {
        var deferredObject = new $.Deferred();
        deferreds.push(deferredObject);
        multipleAjax(i, deferredObject);
    }
    // check if all ajax calls have finished
    $.when.apply($, deferreds).done(function() {
        console.log(results);
        console.log(sValue)
    });
}

JS#2

function getS() {
// --grab URLS to a maximum of 6 --//
for (var i = 0; i < sURLs.length; i++) {
    var sValue = sURLs[i].value;
}

var async2 = $.ajax({
    type: 'GET',
    url: '/' + sValue,
    success: function(data) {
        console.log(data)
    },
    error: function(data) {
        alert('notworking')
    }
})
var async1 = $.ajax({
    type: 'GET',
    url: '/' + sValue,
    success: function(data) {
        console.log(data);
    },
    error: function(data) {
        alert('notworking')
    }
})
var async3 = $.ajax({
    type: 'GET',
    url: '/' + sValue,
    success: function(data) {},
    error: function(data) {
        alert('notworking')
    }
})
$.when(async3, async2, async1).done(function(result3, result2, result1) {});

}

  • 写回答

1条回答 默认 最新

  • weixin_33725807 2017-05-22 16:01
    关注

    Try this:

    // --grab URLS to a maximum of 6 --//
    var sValues = []; //array of urls
    for (var i = 0; i < sURLs.length; i++) {
        sValues[i].push(sURLs[i].value);
    }
    
    function multipleAjax(number, deferredObject) {
            $.ajax({
                url: '/' + sValues[number],
                type: "GET",
                success: function(data) {
                    results.push(data);
                    deferredObject.resolve();
                }
            });
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来