如何在jQuery中比较这些字符串?

时间:2013-03-29 20:46:24

标签: javascript jquery html xml

此程序现在读入xml代码,获取库存缩写,按字母顺序对它们进行排序,然后将它们打印在uo列表中。如果将鼠标悬停在缩写上,颜色将变为红色。我的目标是当你将鼠标悬停在缩写上时,它将显示来自该公司的xml数据的所有数据。我尝试使用if语句说如果符号(xml文件中的缩写)等同于名称(数组中的缩写),那么它会打印出所有垃圾。打印所有内容的行以我想要的格式正常工作。我只需要处理if语句。

我想到的是我无法将两个变量与==进行比较。请记住,symbol也是一个属性,name来自存储符号的数组。我也试着说 - if(checkPassword(name,symbol)) - 然后像我在下面的jQuery代码中那样打印出来,但那不起作用。

我在我正在处理的if语句旁边放了一条注释,它是jQuery的底部。

HTML:

 <body onload="onBodyLoad()">
  <div id="stockList"></div>
  <br />
  <br />
  <br />
  <div id="stockInfo"></div>

jQuery的:

$(document).ready(function () {
    $.ajax({
        type: "GET",
        url: "stocks.xml",
        dataType: "xml",
        success: function (xml) {
            var companyNames = [];
            $(xml).find('Stock').each(function () {
                var symbol = $(this).attr('symbol');
                companyNames.push(symbol);
            });



            companyNames.sort();
            $.each(companyNames, function (index, name) {
                $('#stockList').append('<div><li>' + name + '</li></div>');

            });


            function CheckPassword(val, val2) {

                var strInput = val.value;
                var strInput2 = val2.value;

                if (strInput != strInput2) {
                    val2.focus();
                    val2.select();
                    return false;
                } else
                    return true;

            }


            $(xml).find('Stock').each(function () {
                var company = $(this).find('Company').text();
                var symbol = $(this).attr('symbol');                  
                var market = $(this).find('Market').text();
                var sector = $(this).find('Sector').text();
                var price = $(this).find('Price').text();

                var low = $(this).find('Low').text();
                var high = $(this).find('High').text();


                var amount = $(this).find('Amount').text();
                var yieldx = $(this).find('Yield').text();
                var frequency = $(this).find('Frequency').text();

                $('*').mouseover(function () {
                    $('#stockList li').text($(this).attr('comparison'));
                });


                $('#stockList li').hover(
                function () {
                    $(this).css({ color: 'red' }); //mouseover
                    if (name == symbol) {  // THIS IS THE STATEMENT YOU'RE LOOKING FOR PROGRAMMING GODS

                            $('#stockInfo').append('<div><ol><li>' + "Company = " + company + '</li><br/><li>' + "Market = " + market + '</li><br/><li>' + "Sector = " + sector + '</li><br/><li>' + "Price = " + price + '</li><br/><li>' + "Year Range = " + low + " " + high + '</li></ol><br/>');
                        }

                },
                function () {
                    $(this).css({ color: 'navy' }); // mouseout
                    $('#stockInfo').empty();
                }



            );

            });




        }
    });
});

XML示例:

<Products>
<Stock symbol="GOOG">
    <Company>Google</Company>
    <Market>NASDAQ</Market>
    <Sector>Software</Sector>
    <Price>$487.80</Price>
    <YearRange>
        <Low>$331.55</Low>
        <High>$488.50</High>
    </YearRange>
    <Dividend available="false"/>
</Stock>
<Stock symbol="BA">
    <Company>Boeing Company</Company>
    <Market>NYSE</Market>
    <Sector>Aerospace</Sector>
    <Price>$79.05</Price>
    <YearRange>
        <Low>$63.70</Low>
        <High>$89.58</High>
    </YearRange>
    <Dividend available="true">
        <Amount>$1.20</Amount>
        <Yield>$1.50</Yield>
        <Frequency>QTR</Frequency>
    </Dividend>
</Stock>
<Stock symbol="MO">
    <Company>Altria Group</Company>
    <Market>NYSE</Market>
    <Sector>Comsumables</Sector>
    <Price>$81.70</Price>
    <YearRange>
        <Low>$68.36</Low>
        <High>$85.00</High>
    </YearRange>
    <Dividend available="true">
        <Amount>$3.44</Amount>
        <Yield>$4.2</Yield>
        <Frequency>ANNUAL</Frequency>
    </Dividend>
</Stock>
</Products>

1 个答案:

答案 0 :(得分:0)

        var companyData = [];
        $(xml).find('Stock').each(function () {
            var symbol = $(this).attr('symbol');
            companyNames.push(symbol);
            companyData[symbol] = {
                company: $(this).find('Company').text(),
                symbol: $(this).attr('symbol'),                
                market: $(this).find('Market').text(),
                sector: $(this).find('Sector').text(),
                price: $(this).find('Price').text(),
                low: $(this).find('Low').text(),
                high: $(this).find('High').text(),
                amount: $(this).find('Amount').text(),
                yieldx: $(this).find('Yield').text(),
                frequency: $(this).find('Frequency').text()
            };
        });

        ...

        $("#stocklist li").hover(function() {
            $(this).css({ color: 'red' }); //mouseover
            var info = companyData[$(this).text()];
            $('#stockInfo').append('<div><ol><li>' + "Company = " + info.company + '</li><br/><li>' + "Market = " + info.market + '</li><br/><li>' + "Sector = " + info.sector + '</li><br/><li>' + "Price = " + info.price + '</li><br/><li>' + "Year Range = " + info.low + " " + info.high + '</li></ol><br/>');
        });