2014-07-17 37 views
0

大家好。我的代码有问题。我使用jQuery框架。当我想调用$ .ajax(requestOptions)时,函数xmlParser(xml)不起作用。 我试图找到解决这个问题,但我不能找到任何东西。为什么不工作我的函数xmlParser()。请帮助我

$(document).ready(function() { 

    var requestOptions = { 
    type: "GET", //The method 
    url: "Course_Valute_02-07-2014.xml", //It is reference on xml file 
    dataType: "xml", //The type of data 
    crossDomain: true, //Allow to do the cross-domain request 
    success: xmlParser //Calling function 
    }; 

    function xmlParser(xml) { 
    $("#load").fadeOut(); 
    $(xml).find("Valute").each(function() { 
     $("#outputListValutes").append(
     "<option value=" + $(this).find("CharCode").text() + ">" + $(this).find("CharCode").text() + "</option>"); 
    });  
    }; 

    $.ajax(requestOptions); 

    $("#clear").click(function() { 
    var sumValue = document.getElementById("sum").value = ""; 
    var resValue = document.getElementById("result").value = ""; 
    }); 

    $("#convert").click(function(xml) { 
    //var selectCurrency = $("#inputListCurrency").val(); 
    //findData(xml); 
    }(requestOptions)); 

    function findData(xml) { 

    var decimalOnly = /^\s*-?[1-9]\d*(\.\d{1,2})?\s*$/; 

    try{ 

     var shortName = $("#outputListCurrency").val();            
     var value = $("#sum").val(); 

     if(value == "") throw new Error("Empty value"); 
     else if(!decimalOnly.test(value)) throw new Error("value must be of decimal digits"); 
      else if(value < 0) throw new Error("Value isn't to be below zero"); 
      else if(isNaN(parseFloat(value))) throw new Error("Value isn't to be as symbols"); 

     $(xml).find("Valute").each(function() {               
     if(shortName == $(this).find("CharCode").text()) { 

      var nominal = $(this).find("Nominal").text(); 
      var course = $(this).find("Value").text();  
      var result = parseFloat(value) * parseFloat(nominal)/parseFloat(course);          

      document.getElementById("result").value = Number(result).toFixed(2); 
     } 
     }); 
    } 
    catch(e) { 
     alert(e); 
    } 
    } 
}); 

回答

0

变化使用XMLPARSER功能的请求的成功参数(忘()):

var requestOptions = { 
    type: "GET", //The method 
    url: "Course_Valute_02-07-2014.xml", //It is reference on xml file 
    dataType: "xml", //The type of data 
    crossDomain: true, //Allow to do the cross-domain request 
    success: xmlParser(data) //Calling function 
    }; 
0

我发现这个溶液promlem。我很开心。

var courseFilePath = "xml/Course_Currency_02-07-2014.xml"; 
var listCurrency = []; 

function insertOptions(){ 
    for (var i = 0; i < listCurrency.length; ++i){ 
     $("#outputListCurrency").append(
     "<option value=" + listCurrency[i] + ">" + listCurrency[i] + "</option>"); 
    } 
} 

function xmlParser(xml){ 

    $("#load").fadeOut(); 

    $(xml).find("Valute").each(function(){ 
     var value = $(this).find("CharCode").text(); 
     listCurrency.push(value); 
    }); 

    listCurrency.sort(); 

}; 

function findData(xml){ 

    var decimalOnly = /^\s*-?[0-9]\d*(\.\d{1,2})?\s*$/; 

    try { 

     var shortName = $("#outputListCurrency").val(); 
     var value = $("#sum").val(); 

     if (value == "") throw new Error("Empty value"); 
      else if (!decimalOnly.test(value)) throw new Error("value must be of decimal digits"); 
      else if (value < 0) throw new Error("Value isn't to be below zero"); 
      else if (isNaN(parseFloat(value))) throw new Error("Value isn't to be as symbols"); 

     $(xml).find("Valute").each(function(){ 
      if (shortName == $(this).find("CharCode").text()){ 

       var nominal = $(this).find("Nominal").text(); 
       var course = $(this).find("Value").text(); 
       var result = parseFloat(value) * parseFloat(nominal)/parseFloat(course); 

       document.getElementById("result").value = Number(result).toFixed(2); 
      } 
     }); 
    } 
    catch (e){ 
     alert(e); 
    } 
} 

$(document).ready(function(){ 

    $.ajax({  
    type: "GET", //The method of sending for data 
    url: courseFilePath, //It is reference on xml file 
    dataType: "xml", //The type of data 
    success: function(xml){ 
      xmlParser(xml); 
      insertOptions(); 
     } 
    }); 

    //insertOptions(); 
    $("#clear").click(function() { 
    document.getElementById("sum").value = ""; 
    document.getElementById("result").value = ""; 
    }); 

    $("#convert").click(function() { 
    var selectCurrency = $("#inputListCurrency").val(); 
    $.get(courseFilePath, findData, "xml"); 
    }); 
}); 
相关问题