jQuery& XML奥秘获得数字不是字母 [英] jQuery & XML mystery get Digits NOT Letters

查看:54
本文介绍了jQuery& XML奥秘获得数字不是字母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

BIG EDIT 2012年11月19日



所以,我的问题在这里。
我试图用jQuery和XML开发一个简单的购物车应用程序。



一切都正常,但前提是我的pizz的名字只包含数字。 。 WHY ???



试试你自己,你会坚持认为只有66666披萨正在工作... WHY



工作演示HERE



以下是我的jS代码:

  $(document).ready(function(){
$ .ajax({
type:GET,
url:example.xml,
dataType:($ .browser.msie)?text:xml,
成功:函数(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('< t < 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(' #比萨饼);

});
}
});
});
函数DisPlay(图){
var Display = document.getElementById('Display');
if(图=== null){
Display.value = Figure;
}
else {
Display.value + =++图;
Screen = Display.value
result = eval(Screen);

Display.value = result;
}

图= null;
}
函数GetName(NomPizz){
var $ newItem ='< li>Ajouté:'+ NomPizz +'< / li>';
$('。theList')。append($ newItem);
}

这里是我的XML:

 <?xml version =1.0?> 
<文件>
< row>
< nompizz> 66666< / nompizz>
< petit> 10< / petit>
< moyen> 15< / moyen>
< grand> 20< / grand>
< / row>
< row>
< nompizz>字母< / nompizz>
< petit> 15< / petit>
< moyen> 20< / moyen>
< grand> 25< / grand>
< / row>
< / document>


解决方案

我认为你的问题在于你的 GetAnswer()函数对应用程序的其余部分是不可知的。它不知道被点击的是什么。我认为最简单的做法是将 nom 参数添加到您的 DisPlay()函数调用中。像这样:(删除换行符)

  $('< tr id =bar>< / tr> ').html('< th class ='+ Col0 +'data-item ='+ Col0 +'>'+ Col0 +'< / th> onclick =DisPlay('+ Col1 +',\ '+ Col0 +'\)>'+ Col1 +'€< / td>< td onclick =DisPlay('+ Col2 +',\''+ Col0 +'\)>'+ ('+ Col3 +',\''+ Col0 +'\)>'+ Col3 +''< / td>')。appendTo( '#pizzas'); 

然后您的 DisPlay(图)变成:

 函数DisPlay(图,nom){
var Display = document.getElementById('Display');
if(图=== null){
Display.value = Figure;
}
else
{
Display.value + =++图;

//代替这个 - > //的getAnswer();

//在您的旧代码中,您将这些变量中的一部分声明为非全局变量,但像使用全局变量一样使用它们
Screen = Display.value.replace(/' /G, ' ');
result = eval(Screen);
Display.value = result;
$('。theList')。append('< li>Ajouté:'+ nom +'< / li>');
}
图= null;
}

关于全局变量与非全局变量的关系。如果你在一个函数中'var'东西,那个变量对于那个函数是本地的。你似乎在混合和匹配你的变数。有关阅读此文章的更多信息。

编辑:

在您进行新的编辑之后,您只能获取数字比萨名称,因为您需要引用int他适当的地方。尝试将所有 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 +') '+ Col'+'');>'+ Col2 +''< / td>< td onclick =DisPlay('+ Col3 +'); GetName(\''+ Col0 +'\' );> '+ COL3 +' €< / TD> ')appendTo(' #比萨饼); 


BIG EDIT 11/19/2012

So, my problem is here. I'm trying to develop a simple cart app with jQuery and XML.

Everything's OK, but only if the name of My pizz only contains numbers... WHY ???

Try yourself, you'll constat that only the "66666" pizza is working... WHY ???

WORKING DEMO HERE

Here is my jS code:

$(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); 
}

And here my 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>

解决方案

I think your problem is that your GetAnswer() function is agnostic to the rest of the application. It doesn't know what was clicked. I think that the easiest thing to do would be to just add the nom parameter to your DisPlay() function call. Like this: (remove the linefeeds)

$('<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');

Then your DisPlay(Figure) becomes:

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;
}

A word about global variables vs. non-global variables. If you 'var' something inside a function, that variable is local to that function. It seems like you were mixing and matching your vars. For more on that read this post.

EDIT:

After your new edits you are only getting numeric pizza names because you need to have quotes int he proper place. Try changing all your GetName('+Col0+'); to GetName(\''+Col0+'\');

So that whole line would read:

$('<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');

这篇关于jQuery&amp; XML奥秘获得数字不是字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆