简体   繁体   English

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

[英]How can I compare these strings in jQuery?

This program right now reads in xml code, gets a stock abbreviation, alphabetically sorts them, and then prints them out in an uo list. 该程序现在读取xml代码,得到股票简称,按字母顺序对它们进行排序,然后将它们打印在uo列表中。 If you hover over the abbreviations the color will change to red. 如果将鼠标悬停在缩写上,则颜色将变为红色。 The goal I'm having is when you hover over an abbreviation, it will show all the data from the xml data just for that company. 我的目标是当您将鼠标悬停在缩写上时,它将显示该公司的xml数据中的所有数据。 I tried using the if statement saying if the symbol (abbreviation in xml file) is equivalent to the name (abbreviation in array) then it prints out all the junk for it. 我尝试使用if语句说如果符号(xml文件中的缩写)与名称(数组中的缩写)等效,则它会为此打印所有垃圾。 The line that prints everything out works correctly in the format I want. 打印出所有内容的行以我想要的格式正确工作。 I just need to work on the if statement. 我只需要处理if语句。

What I have figured out is I cannot compare two variables with the ==. 我发现的是我无法将两个变量与==进行比较。 Keep in mind symbol is an attribute as well, and name is from an array that stores the symbols. 请记住,符号也是属性,名称是来自存储符号的数组。 I also tried just saying - if(checkPassword(name, symbol)) - and print it all out as I did in the jQuery code below, but that did not work. 我还尝试过说-if(checkPassword(name,symbol))-并像我在下面的jQuery代码中一样将其全部打印出来,但是那没有用。

I put a comment next to the if statement I am working on, it's towards the bottom of the jQuery. 我在正在处理的if语句旁边添加了一条注释,它位于jQuery的底部。

HTML: HTML:

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

jQuery: 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 sample: 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>
        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/>');
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM