首页 文章

在其他功能完成后监听功能

提问于
浏览
0

我正在使用RS-JAX GET请求加载html . 在请求之后,我需要函数二来开始监听输入 .

下面是我尝试过的,但是现在我遇到两个错误:1)TypeError:functionTwo不是函数 . 2)ReferenceError:未定义loadIngredients .

我对#2的猜测是我在另一个函数中调用它并且var functionOne正在弄乱它 . 我不知道#1 .

有没有办法来解决这个问题?也许通过编辑代码,也许通过使用另一种方式来监听功能一?

功能一

var functionOne = function loadIngredients(selected) {  
    var r = $.Deferred();
    var username = window.sessionStorage.getItem("huidigeGebruiker");
    var datum = document.getElementById("datepicker").value;
    var url = "restservices/ingredients?Q1=" + username + "&Q2=" + datum;
        $.ajax({
            url : url,
            method : "GET",
            beforeSend : function(xhr) {
                var token = window.sessionStorage.getItem("sessionToken");
                xhr.setRequestHeader('Authorization', 'Bearer ' + token);
            },
            success : function(data) {
                $(".table").find("tr:gt(0):not(:last)").remove();
                 $(data).each(function (index) {
                     $(".table").find('tr:last').prev().after('<tr><td class="ingredient">'+this.ingredientnaam+'</td><td class="hoeveelheid"><input class="gramtxt" type="text" value="'+this.hoeveelheid+'" name="gramtxt" id="gramtxt"></td><td class="subtotaal">'+this[selected]+'</td><td class="removeingredient"><a href="#"> <i class="fa fa-times text-red"></i></a></td></tr>');
                    });
                 loadTotals();
                 ingredienten = [];
                 loadAllIngredients();
            },
            error: function(xhr){
                $(".dagboektable").html('Ophalen ingrediënten mislukt. Ben je ingelogd?');
            },
        });
        return r;
}

功能二

var functionTwo = $('#gramtext').bind('input', function() { 
    console.log("Y");
    var hoeveelheid = $(this).val();
    console.log(hoeveelheid);
});

倾听者

functionOne().done( functionTwo() );

1 回答

  • 0

    Things needs to be changed :

    First :

    var functionOne = function loadIngredients(selected) { 
    To
    var functionOne = function (selected) {
    

    Second :

    $.ajax(......); return r;
    TO
    return $.ajax(......);
    

    Third :

    var functionTwo = $('#gramtext').bind('input', function() { 
        console.log("Y");
        var hoeveelheid = $(this).val();
        console.log(hoeveelheid);
    });
    
    TO
    
    var functionTwo = function(){
     $('#gramtext').bind('input', function() { 
        console.log("Y");
        var hoeveelheid = $(this).val();
        console.log(hoeveelheid);
     });
    }
    

    这将解决几乎所有的错误,然后再试一次 .

相关问题