大编辑11/19/2012
所以,我的问题就在这里.我正在尝试使用jQuery和XML开发一个简单的购物车应用程序.
一切都还可以,但只有My Pizz的名字只包含数字...... 为什么???
尝试自己,你会认为只有"66666"比萨饼正在运作...... 为什么???
这是我的jS代码:
$(document).ready(function(){
$.ajax({
type: "GET",
url: "example.xml",
dataType: ($.browser.msie) ? "text" : "xml",
success: function(xml) {
$(xml).find('row').each(function(){
var Col0 = $(this).find('nompizz').text();
var Col1 = $(this).find('petit').text();
var Col2 = $(this).find('moyen').text();
var Col3 = $(this).find('grand').text();
$('<tr id="bar"></tr>').html('<th class="title">'+Col0+'</th><td onclick="DisPlay('+Col1+');GetName('+Col0+');">'+Col1+'€</td><td onclick="DisPlay('+Col2+');GetName('+Col0+');">'+Col2+'€</td><td onclick="DisPlay('+Col3+');GetName('+Col0+');">'+Col3+'€</td>').appendTo('#pizzas');
});
}
});
});
function DisPlay(Figure) {
var Display = document.getElementById('Display');
if (Figure === null) {
Display.value = Figure;
}
else {
Display.value += "+"+Figure;
Screen = Display.value
result = eval(Screen);
Display.value =result;
}
Figure = null;
}
function GetName(NomPizz) {
var $newItem = '<li>Ajouté: '+NomPizz+'</li>';
$('.theList').append($newItem);
}
Run Code Online (Sandbox Code Playgroud)
在这里我的XML:
<?xml version="1.0"?>
<document>
<row>
<nompizz>66666</nompizz>
<petit>10</petit >
<moyen>15</moyen >
<grand>20</grand>
</row>
<row>
<nompizz>Letters</nompizz >
<petit>15</petit >
<moyen>20</moyen >
<grand>25</grand>
</row>
</document>
Run Code Online (Sandbox Code Playgroud)
我认为您的问题是您的GetAnswer()功能与应用程序的其余部分无关.它不知道点击了什么.我认为最简单的方法是将nom参数添加到DisPlay()函数调用中.像这样:(删除换行)
$('<tr id="bar"></tr>').html('<th class='+Col0+' data-item='+Col0+'>'+Col0+'</th> <td onclick="DisPlay('+Col1+',\"'+Col0+'\")">'+Col1+'€</td> <td onclick="DisPlay('+Col2+',\"'+Col0+'\")">'+Col2+'€</td> <td onclick="DisPlay('+Col3+',\"'+Col0+'\")">'+Col3+'€</td>').appendTo('#pizzas');
Run Code Online (Sandbox Code Playgroud)
然后你DisPlay(Figure)变成:
function DisPlay(Figure, nom) {
var Display = document.getElementById('Display');
if (Figure === null) {
Display.value = Figure;
}
else
{
Display.value += "+"+Figure;
//instead of this --> //GetAnswer();
//in your old code you had some of these variables declared as non-global variables, but were using them like they were global variables
Screen = Display.value.replace(/'/g, ' ');
result = eval(Screen);
Display.value = result;
$('.theList').append('<li>Ajouté: '+nom+'</li>');
}
Figure = null;
}
Run Code Online (Sandbox Code Playgroud)
关于全局变量与非全局变量的关系.如果你在函数内部'var',那么该变量是该函数的本地变量.看起来你正在混合和匹配你的vars.有关详细信息,请阅读此帖.
编辑:
在您进行新的编辑后,您只能获得数字披萨名称,因为您需要在适当的位置使用引号.尝试将所有内容更改GetName('+Col0+');为GetName(\''+Col0+'\');
所以整行都会读到:
$('<tr id="bar"></tr>').html('<th class="title">'+Col0+'</th><td onclick="DisPlay('+Col1+');GetName(\''+Col0+'\');">'+Col1+'€</td><td onclick="DisPlay('+Col2+');GetName(\''+Col0+'\');">'+Col2+'€</td><td onclick="DisPlay('+Col3+');GetName(\''+Col0+'\');">'+Col3+'€</td>').appendTo('#pizzas');
Run Code Online (Sandbox Code Playgroud)