首页 > JavaScript如何比较字符串的大小呢,请大神赐教

JavaScript如何比较字符串的大小呢,请大神赐教

<script type="text/javascript">
            var xmlHttp;
            function createXMLHttpRequest(){
                if(window.ActiveXObject){
                    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
                }else if(window.XMLHttpRequest){
                    xmlHttp = new XMLHttpRequest();
                }
            }
            
            function doSearch(){
                createXMLHttpRequest();
                xmlHttp.onreadystatechange = handleStateChange;
                xmlHttp.open("GET","dynamicContent.xml",true);
                xmlHttp.send(null);
            }
            
            function handleStateChange(){
                if(xmlHttp.readyState == 4){
                    if(xmlHttp.status == 200){
                        clearPreviousResults();
                        parseResults();
                    }
                }
            }
            
            function clearPreviousResults(){
                var header = document.getElementById("header");
                if(header.hasChildNodes()){
                    header.removeChild(header.childNodes[0]);
                }
                var tableBody = document.getElementById("resultsBody");
                while(tableBody.childNodes.length > 0){
                    tableBody.removeChild(tableBody.childNodes[0]);
                }
            }
            
            function parseResults(){
                var results = xmlHttp.responseXML;
                var property = null;
                var address = "";
                var price = "";
                var comments = "";
                
                var starvalue = document.getElementById("starsect").value;
                var endvalue = document.getElementById("endsect").value;
                
                var properties = results.getElementsByTagName("property");
                for (var i = 0;i < properties.length;i++){
                    property = properties[i];
                    price = property.getElementsByTagName("price")[0].firstChild.nodeValue;
                    //if(parseInt(price,10) >= parseInt(starvalue,10) && parseInt(price,10) <= parseInt(endvalue,10)){
                        address = property.getElementsByTagName("address")[0].firstChild.nodeValue;
                        comments = property.getElementsByTagName("comments")[0].firstChild.nodeValue;
                        addTableRow(address,price,comments);
                    //}

                }
                var header = document.createElement("h2");
                var headerText = document.createTextNode("ReSults:");
                
                header.appendChild(headerText);
                document.getElementById("header").appendChild(header);
                document.getElementById("resultsTable").setAttribute("border","1");
            }
            
            function addTableRow(address,price,comments){
                var row = document.createElement("tr");
                var cell = createCellWithText(address);
                row.appendChild(cell);
                
                cell = createCellWithText(price);
                row.appendChild(cell);
                
                cell = createCellWithText(comments);
                row.appendChild(cell);
                
                document.getElementById("resultsBody").appendChild(row);
            }
            
            function createCellWithText(text){
                var cell = document.createElement("td");
                var textNode = document.createTextNode(text);
                cell.appendChild(textNode);
                return cell;
            }
        </script>

打开 //if(parseInt(price,10) >= parseInt(starvalue,10) && parseInt(price,10) <= parseInt(endvalue,10))的注释就报错,我本想加个判断,只显示在选择范围之间的内容,即显示的信息的price在starvalue和endvalue之间

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Dynamically Editing Page Content</title>
        <script type="text/javascript">
            var xmlHttp;
            function createXMLHttpRequest(){
                if(window.ActiveXObject){
                    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
                }else if(window.XMLHttpRequest){
                    xmlHttp = new XMLHttpRequest();
                }
            }
            
            function doSearch(){
                createXMLHttpRequest();
                xmlHttp.onreadystatechange = handleStateChange;
                xmlHttp.open("GET","dynamicContent.xml",true);
                xmlHttp.send(null);
            }
            
            function handleStateChange(){
                if(xmlHttp.readyState == 4){
                    if(xmlHttp.status == 200){
                        clearPreviousResults();
                        parseResults();
                    }
                }
            }
            
            function clearPreviousResults(){
                var header = document.getElementById("header");
                if(header.hasChildNodes()){
                    header.removeChild(header.childNodes[0]);
                }
                var tableBody = document.getElementById("resultsBody");
                while(tableBody.childNodes.length > 0){
                    tableBody.removeChild(tableBody.childNodes[0]);
                }
            }
            
            function parseResults(){
                var results = xmlHttp.responseXML;
                var property = null;
                var address = "";
                var price = "";
                var comments = "";
                
                var starvalue = document.getElementById("starsect").value;
                var endvalue = document.getElementById("endsect").value;
                
                var properties = results.getElementsByTagName("property");
                for (var i = 0;i < properties.length;i++){
                    property = properties[i];
                    price = property.getElementsByTagName("price")[0].firstChild.nodeValue;
                    if(price >= starvalue && price <= endvalue){
                        address = property.getElementsByTagName("address")[0].firstChild.nodeValue;
                        comments = property.getElementsByTagName("comments")[0].firstChild.nodeValue;
                        addTableRow(address,price,comments);
                    }

                }
                var header = document.createElement("h2");
                var headerText = document.createTextNode("ReSults:");
                
                header.appendChild(headerText);
                document.getElementById("header").appendChild(header);
                document.getElementById("resultsTable").setAttribute("border","1");
            }
            
            function addTableRow(address,price,comments){
                var row = document.createElement("tr");
                var cell = createCellWithText(address);
                row.appendChild(cell);
                
                cell = createCellWithText(price);
                row.appendChild(cell);
                
                cell = createCellWithText(comments);
                row.appendChild(cell);
                
                document.getElementById("resultsBody").appendChild(row);
            }
            
            function createCellWithText(text){
                var cell = document.createElement("td");
                var textNode = document.createTextNode(text);
                cell.appendChild(textNode);
                return cell;
            }
        </script>
    </head>
    <body>
        <h1>Search Real Estate Listings</h1>
        <form action="#">
            Show listings from 
            <select id="starsect">
                <option value="50000">$50,000</option>
                <option value="100000">$100,000</option>
                <option value="150000">$150,000</option>
            </select>
            to
            <select id="endsect">
                <option value="100000">$100,000</option>
                <option value="150000">$150,000</option>
                <option value="200000">$200,000</option>
            </select>
            <input type="button" value="Search" onclick="doSearch();"/>
        </form>
        
        <span id="header"> 
        </span>
        
        <table id="resultsTable" width="75%" border="0">
            <tbody id="resultsBody">
            </tbody>
        </table>
    </body>
</html>

xml:

<?xml version="1.0" encoding="UTF-8"?>
<properties>
    <property>
        <address>812 Gwyn Ave</address>
        <price>$100,000</price>
        <comments>Quiet, serene neighborhood</comments>
    </property>
    <property>
        <address>3308 James Ave S</address>
        <price>$110,000</price>
        <comments>Close to schools, shopping, entertainment</comments>
    </property>
    <property>
        <address>983320 County Rd 113</address>
        <price>$115,000</price>
        <comments>Small acreage outside of town</comments>
    </property>
</properties>

如果你这里获取到price,starvalue,envalue都是纯数字的字符串的话,那么不需要parseInt转换,直接比较即可,javascript会自动转成数字后再进行比较的;还有parseInt(price , 10)得到的结果可能为price + 10后的值... 如果不能解决,可以将错误抛出来,看报了什么样的错误!!


需要把$,过滤掉;parseInt 遇到特殊字符停止解析的 - x 等除外

price = price.replace(/[~'!<>@#$%,^&*()+_=:]/g, "");
                    if(parseInt(price,10) >= parseInt(starvalue,10) && parseInt(price,10) <= parseInt(endvalue,10)){
                        address = property.getElementsByTagName("address")[0].firstChild.nodeValue;
                        comments = property.getElementsByTagName("comments")[0].firstChild.nodeValue;
                        addTableRow(address,price,comments);
                    }
【热门文章】
【热门文章】