Issue loading script, unless alert() is pr

2019-09-09 10:55发布

问题:

I'm having an issue running all of the jquery code in one of the .js files I created. The following code calculates the price of metals by the pound, according to a list which is populated through AJAX. This code runs fine on my own server, but as soon as I implement it into my clients WordPress page, it breaks. Here is the code:

$(document).ready(function(){

    $.get("wp-parse-list.php",function(ajaxresult){

         $(ajaxresult).insertAfter(".purchase-calc-header");

    });

});


$(document).ready(function(){

    $(".calc-input").keyup(function() {
        var priceTexts = [];

            $(".metalPrice").each(function() { 
                priceTexts.push($(this).text().replace("$", "")); 
            });

        var ammountInd = [];
        var i = 0;

            $(".calc-input").each(function() {
                ammountInd.push($(this).val() * priceTexts[ i ]);
                i = i + 1;
            });

        var total = 0;
            $.each(ammountInd,function() {
                    total += this;
            });


        var total = total.toFixed(2);
            $('.total-price').text('$' + total);    

        if (total > 99.99) {
            $('.free-pickup').text("This Order Qualifies for Free Pick Up").css('color', 'green');
        } else {
            $('.free-pickup').text("This Order Does Not Qualify for Free Pick Up").css('color', 'red');
        }

    });



});

$(function () {
        $(".calc-input").keydown(function (event) {


            if (event.shiftKey == true) {
                event.preventDefault();
            }

            if ((event.keyCode >= 48 && event.keyCode <= 57) || (event.keyCode >= 96 && event.keyCode <= 105) || event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 37 || event.keyCode == 39 || event.keyCode == 46 || event.keyCode == 190) {

            } else {
                event.preventDefault();
            }

            if($(this).val().indexOf('.') !== -1 && event.keyCode == 190)
                event.preventDefault();

        });
});

Now, I did noticed that if I insert an alert() tag into the code (anywhere), it causes a pause in the wordpress page load, and allows the DOM's and function to be loaded in. Unfortunately, I cannot have an alert displayed on this page just to have this function properly. Rather than back tracking the issue to something within the wordpress template, or a plugin, is there a way to prevent these events from occurring until all other events have finished loading? Any suggestions how how to solve this will be greatly appreciated!