1

这是我第一次来这里寻求您的帮助和知识!我对 AJAX 脚本和添加“if”条件以从 XML 下载文件有疑问。

一般来说,脚本本身可以工作,但没有我需要以某种方式添加的函数“if”。我在谷歌上寻求一些帮助,但没有结果:(如果我理解得很好,应该添加一个条件“如果,否则”,但我不知道该怎么做,所以我非常感谢你的帮助。如果有人能够为我提供帮助并添加此代码,我将不胜感激!

// this is not active now becouse i don't use this xml file
$(document).ready(function() {
    $.ajax({
        type: "GET",
        url: "premier-rates.xml",
        dataType: "xml",
        success: parseXml
    });
    // this is my XML file
    $.ajax({
        type: "GET",
        url: "go-rates.xml",
        dataType: "xml",
        success: parseXml_go
    });

    $("#selCountry").change(onSelectChange);
    $("#selCountry_go").change(onSelectChange_go);

});

function gup(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null) return "";
    else return results[1];
}

function parseXml(xml) {

    $(xml).find("db_rates").each(function() {
        var this_countryname = $(this).find("country").text();
        var this_id = $(this).find("id").text();
        var curOption = $("<option value='" + this_id + "'>" + this_countryname + "</option>")
        curOption.data("db_rates", $(this)); // bind the country node data from the XML to the                                             option of the dropdown
        $(curOption).appendTo($('#selCountry'))
    });

}

function onSelectChange() {
    var selected = $('#selCountry').children(':selected').data('db_rates');

    $('#rates_output').html("");
    var this_country = $(selected).find('country').text();
    var this_landlineRates = $(selected).find('landline_rates');
    var this_mobileRates = $(selected).find('mobile_rates');

    $("#rates_output").append("<p class='dest_title'>" + this_country + "</p>");
    $("#rates_output").append("<ul><li><div class='dest_list'>Landline</div><div class='dest_list'>" + this_landlineRates.text() + "p /min</div><div class='clear'></div></li><li><div class='dest_list'>Mobile</div><div class='dest_list'>" + this_mobileRates.text() + "p /min</div><div class='clear'></div></li></ul>");

    //$("#rates_output").append("Landline: " + this_landlineRates.text() + "p /min<br />     Mobile: " + this_mobileRates.text() +"p /min");
    this_country = this_country.replace(/ /gi, "-");

    var flagoutput = "";
    var this_id = gup('id');

    if (this_id == "1") {
        flagoutput = "<img src='images/flag/" + this_country + ".png' style='float:right;     border:1px solid #CCC; margin-right:10px; ' />";
    } else {
        flagoutput = "<img src='images/flag/" + this_country + ".png' style='float:left; border:1px solid #CCC; ' />";
    }
    $("#flagoutput").html(flagoutput);
}

function parseXml_go(xml) {

    $(xml).find("db_rates").each(function() {
        var this_countryname = $(this).find("country").text();
        var this_id = $(this).find("id").text();
        var curOption = $("<option value='" + this_id + "'>" + this_countryname + "</option>")
        curOption.data("db_rates", $(this)); // bind the country node data from the XML to the     option of the dropdown
        $(curOption).appendTo($('#selCountry_go'))
    });

}

function onSelectChange_go() {
    var selected = $('#selCountry_go').children(':selected').data('db_rates');

    $('#rates_output_go').html("");
    var this_country = $(selected).find('country').text();

    //THOSE TWO VARIABLE ARE ALWAYS LOADED
    var this_landlineRates = $(selected).find('landline_rates');
    var this_mobileRates = $(selected).find('mobile_rates');



    // THOSE VARIABLES I WOULD LIKE TO ADD TO MY WEBSITE (DEPENDS ON HOW MANY OPERATORS ARE IN THE COUNTRY... SO IT CAN BE: this_mobileRatesC, D, E... 
    var this_mobileRatesA = $(selected).find('mobile_ratesA');
    var this_mobileRatesB = $(selected).find('mobile_ratesB');




    // AGAIN - THOSE TWO VARIABLES ARE ALWAYS LOADED
    $("#rates_output_go").append("<p class='dest_title'>" + this_country + "</p>");
    $("#rates_output_go").append("<ul><li><div class='dest_list'>Landline</div><div class='dest_list'>" + this_landlineRates.text() + "p /min</div><div class='clear'></div></li><li><div class='dest_list'>Mobile</div><div class='dest_list'>" + this_mobileRates.text() + "p /min</div><div class='clear'></div></li></ul>");


    // SO I WAS TRYING TO ADD MY CODE, BUT I THINK SOMETHING IS WRONG AS IT ALWAYS SHOWS VALUE EVEN IF THE VALUE IS 0 OR DOES NOT EXIST IN MY XML FILE (it will be more for this_mobileRatesB, C, D...)
    var this_mobileRatesA = $(selected).find('mobile_ratesA');
    if ('mobile_ratesA' >= "0") {
        $("#rates_output_go").append("<ul><li><div class='dest_list'>A</div><div class='dest_list'>" + this_mobileRatesA.text() + "p /min</div><div class='clear'></div></li></ul>");

        // for more added this_mobileRatesB
        var this_mobileRatesA = $(selected).find('mobile_ratesA');
        if ('mobile_ratesB' >= "0") {
            $("#rates_output_go").append("<ul><li><div class='dest_list'>A</div><div class='dest_list'>" + this_mobileRatesB.text() + "p /min</div><div class='clear'></div></li></ul>");



            this_country = this_country.replace(/ /gi, "-");

            var flagoutput = "";

            /* */
            var this_id = gup('id');

            if (this_id == "1") {
                flagoutput = "<img src='images/flag/" + this_country + ".png' style='float:right;     border:1px solid #CCC;margin-right:10px; ' />";
            } else {
                flagoutput = "<img src='images/flag/" + this_country + ".png' style='float:left; border:1px solid #CCC; ' />";
            }
            $("#flagoutput").html(flagoutput);
        }
    }
}

请注意:添加评论只是为了使这篇文章更清晰

XML 文件 Premier-rates.xml

<?xml version="1.0" encoding="utf-8" ?>
<Rates>
<db_rates>
<id>1</id>
<country>country1</country>
<landline_rates>0.10</landline_rates>
<mobile_rates>0.15</mobile_rates>
<mobile_ratesA>0.50</mobile_ratesA> // I WONT IF HAVE VALUE IS APPENDED
<mobile_ratesB></mobile_ratesB> // I WONT IF 0 OR EMPTY IS NOT APPENDED
</db_rates>
<db_rates>
... // MORE COUNTRIES
</db_rates>
...
</Rates>

请你能帮我解决这个问题,不一定是我试图这样做的方式,因为可能有更好的方式来做到这一点。我将不胜感激任何帮助和建议。

多谢

4

2 回答 2

0

不确定您要问的全部内容,但如果我是正确的,您只是希望做一个有条件的 XML 文件吗?AKA 如果一个条件产生真正的拉 xml 文件 A,如果不是拉 xml 文件 B?

如果是这样,您可以这样做:更改以下代码片段

$.ajax({
type: "GET",
url: "premier-rates.xml",
dataType: "xml",
success: parseXml   
});
// this is my XML file
$.ajax({
type: "GET",
url: "go-rates.xml",
dataType: "xml",
success: parseXml_go    
});

成为:

var file_data = {};
var download_xml_file_A = true; //This can be any type of conditional, just using an example
if(download_xml_file_A) {
    file_data.filename = "premier-rates.xml";
    file_data.callback = parseXml;
}
else {
    file_data.filename = "go-rates.xml";
    file_data.callback = parseXml_go;
}

$.ajax({
    type: "GET",
    url: file_data.filename,
    dataType: "xml",
    success: file_data.callback
    });

希望这可以帮助!

于 2012-08-06T22:25:19.447 回答
0

这是第一个引起我注意的事情:

// SO I WAS TRYING TO ADD MY CODE, BUT I THINK SOMETHING IS WRONG AS IT ALWAYS SHOWS VALUE EVEN IF THE VALUE IS 0 OR DOES NOT EXIST IN MY XML FILE (it will be more for this_mobileRatesB, C, D...)
var this_mobileRatesA = $(selected).find('mobile_ratesA');
if ('mobile_ratesA' >="0"){
$("#rates_output_go").append("<ul><li><div class='dest_list'>A</div><div class='dest_list'>" + this_mobileRatesA.text() + "p /min</div><div class='clear'></div></li></ul>");

// for more added this_mobileRatesB
var this_mobileRatesA = $(selected).find('mobile_ratesA');
if ('mobile_ratesB' >="0"){
$("#rates_output_go").append("<ul><li><div class='dest_list'>A</div><div class='dest_list'>" + this_mobileRatesB.text() + "p /min</div><div class='clear'></div></li></ul>");

if ('mobile_ratesA' >="0")您比较两个常量字符串。这个结果永远不会改变,所以这个值总是会被​​打印出来(因为'mobile_ratesA' >= '0'通过字符串比较)。

我相信你想要的是

// only print if we find at least one mobileRatesA node
if(this_mobileRatesA.get().length > 0) {
    ...
}

也许

// only print if mobileRatesA contains a string
// representation of a number greater than 0
if(parseInt(this_mobileRatesA.text()) > 0) {
    ...
}
于 2012-08-06T22:21:56.627 回答