我在哪里遇到这个Jquery代码?

时间:2013-01-31 23:07:28

标签: jquery jquery-ui javascript javascript-framework

好的,所以这段代码以前工作过...但事实是,在这种HTML格式中,人们将输入以0-开头的数字,例如0-1299999,并自动转换为1299999,并包含转换函数将函数从字符串转换为整数..它曾经在用户输入数字并验证上面的范围并填充菜单之前工作。我在哪里出错了?它不再自动填充选择菜单。

<html>
    <head>
    <script type="text/javascript" src="jquery/jquery-1.8.3.js"></script>
    <script type="text/javascript">
    function numberFromInput(value) {
        return parseInt(value.match(/[-]?(\d*)$/).pop());
    }
$(function () {
    var ranges = {
        0 : {
            min: 986329,
            max: 999999,
            StxName: "stx2",
            rtumodel: "globalstar"
        },
        1 : {
            min: 660000,
            max: 699999,
            StxName: "mmt",
            rtumodel: "globalstar"
        },
        2 : {
            min: 200000,
            max: 299999,
            StxName: "stm3",
            rtumodel: "stmcomtech"
        },
        3 : {
            min: 1202114,
            max: 1299999,
            StxName: "smartone",
            rtumodel: "globalstar"
        }
    };

    $(":text").css("border", "2px solid red");

    $(":text").change(function(){
        var enteredData = numberFromInput($(this).val());

        if (enteredData == "") {
          $(this).css("border", "2px solid red");
        } else {
          $(this).css("border", "inherit");
        }

        if ($(this).attr("id") == "ESNList") {
            var esnList = enteredData.split(",");
            var item;
            var foundRange;
            var rangesFound = [];

            for (var i = 0; i < esnList.length; i++) { // Loop over entries in the text field
                rangesFound[i] = -1; // Set defaut value as -1 for the corresponding entry in the "rangesFound" array
                for (var x in ranges) { // Inner loop over items in "ranges" object
                    item = parseInt(esnList[i]);
                    if (item >= ranges[x].min && item <= ranges[x].max) {
                        rangesFound[i] = x;
                    }
                }
            }

            if ( rangesFound[0] >= 0 ) {  // Loop over rangesFound to evaluate if any of the values do not match the first entry
                foundRange = rangesFound[0];
                for ( var y = 1; y < rangesFound.length; y++ ){
                    if (rangesFound[y] !== foundRange) {
                        foundRange = undefined;
                    }
                }
            } else { // In this case the first entry was not found in the available ranges
                foundRange = undefined;
            }

            if (foundRange && foundRange >= 0) {
                $("#STxModel").val(ranges[foundRange].StxName);
                $("#TxSpacing").val(ranges[foundRange].rtumodel);
            } else {
                alert("ESNs should be withing the same range");
            }            
        }
        if ($(this).attr("id") == "ESNStart"){
        var esnStart  = [];
            esnStart[0] = parseInt(enteredData);
            var item;
            var foundRange;
            var rangesFound = [];

            for (var i = 0; i < esnStart.length; i++) { // Loop over entries in the text field
                rangesFound[i] = -1; // Set defaut value as -1 for the corresponding entry in the "rangesFound" array
                for (var x in ranges) { // Inner loop over items in ranges object
                    item = parseInt(esnStart[i]);
                    if (item >= ranges[x].min && item <= ranges[x].max) {
                        rangesFound[i] = x;
                    }
                }
            }

            if ( rangesFound[0] >= 0 ) {  // Loop over rangesFound to evaluate if any of the values do not match the first entry
                foundRange = rangesFound[0];
                for ( var y = 1; y < rangesFound.length; y++ ){
                    if (rangesFound[y] !== foundRange) {
                        foundRange = undefined;
                    }
                }
            } else { // In this case the first entry was not found in the available ranges
                foundRange = undefined;
            }

            if (foundRange && foundRange >= 0) {
                $("#STxModel").val(ranges[foundRange].StxName);
                $("#TxSpacing").val(ranges[foundRange].rtumodel);
            }           
        }
        if ($(this).attr("id") == "ESNEnd"){
        var esnEnd = [];
         esnEnd[0] = parseInt(enteredData);
            var item;
            var foundRange;
            var rangesFound = [];

            for (var i = 0; i < esnEnd.length; i++) { // Loop over entries in the text field
                rangesFound[i] = -1; // Set defaut value as -1 for the corresponding entry in the rangesFound array
                for (var x in ranges) { // Inner loop over items in ranges object
                    item = parseInt(esnEnd[i]);
                    if (item >= ranges[x].min && item <= ranges[x].max) {
                        rangesFound[i] = x;
                    }
                }
            }

            if ( rangesFound[0] >= 0 ) {  // Loop over rangesFound to evaluate if any of the values do not match the first entry
                foundRange = rangesFound[0];
                for ( var y = 1; y < rangesFound.length; y++ ){
                    if (rangesFound[y] !== foundRange) {
                        foundRange = undefined;
                    }
                }
            } else { // In this case the first entry was not found in the available ranges
                foundRange = undefined;
            }

            if (foundRange && foundRange >= 0) {
                $("#STxModel").val(ranges[foundRange].StxName);
                $("#TxSpacing").val(ranges[foundRange].rtumodel);
            }           
        }

   });
});
</script> </head>
    <body>
    <form id="provision">
        ESNList:    <input  type="text" id="ESNList" name="ESNList" size="30" /> <br />
        ESN Start:<input type="text" id="ESNStart" name="ESNStart" size="10" /> <br />
        ESN End: <input type="text" id="ESNEnd" name="ESNStart" size="10" /> <br />
        UnitName:<input type="text" id="STxName" name="STxName" size="30"  />  <br />  
         Unit Model:   <select name="STxName" id="STxModel">
        <option value="stx2">STX2</option>
        <option value="stm3" selected>STM3</option>
        <option value="acutec">Acutec</option>
         <option value="trackpack">Trackpack</option>
        <option value="mmt">MMT</option>
        <option value="smartone">Smartone</option>
        <option value="smartoneb" >SmartOneB</option>
        </select> <br />
        RTU Model Type:
         <select name="rtumodel" id ="TxSpacing">
        <option value="globalstar">GlobalStar</option>
        <option value="both">Both</option>
        <option value="comtech">Comtech</option>
        <option value="stmcomtech">STMComtech</option>
        </select> <br />
        <input type="submit" value ="submit"  />
        </form>
    </body>
    </html> 

0 个答案:

没有答案