2

我有一个从 JSON 文件自动填充的列表:

<div class="conversionFormES" id="from1">
            <label for="from">Convert From:</label>
                <select size = "10" name="from" id="from" onchange="convertUnits()">
                    <option value="from">-Select an Option-</option>
                    <option value="from">Firefox</option>
                </select>
</div>

我添加了“Firefox”选项只是为了确保正在显示某些内容。

任何想法可能是什么问题。只是为了重新迭代,它与 chrome 和 safari 完美配合。非常感谢。

转换单位函数:

function convertUnits(){

var convertObj = new Convert($("#from"), $("#to"), $("#units"), $("#result"));
var measurementType = $("#from option:selected").attr("class");
var result = "invalid input";
var input = parseInt(convertObj.units.val());

// convertObj.convertUnitsFromHash();
if(measurementType == "temp"){
    convertObj.convertTemp();

}else{

    convertObj.convertUnitsFromHash();
}
console.log('Measurement Type:', measurementType);

}

在 HTML 表单之后调用的 JSON 脚本。

        <script>
        //        JSON:
        //        The key is the class identifier, temp, area etc etc
        //        Value is being used for both ID and Value when the list is being populated
        $(document).ready(function(){

            $.getJSON('JSON/conversionJSON.json', function(data){

                console.log(data);
                //for testing output only
                var list = $("<ul />");

                $.each(data, function (key, conversions) {
                    console.log(key + ":" + conversions);

                    $.each(conversions, function (index, conversion) {
                        console.log("<li>Name: " + conversion.name + " :Value: " + conversion.value + "</li>");

                        if(key == "<?php echo $conversionType ?>"){

                            $("#from").append('<option class="'+key+'" id="'+conversion.value+'" value="'+conversion.value+'">'+conversion.name+'</option>');
                            $("#to").append('<option class="'+key+'" id="'+conversion.value+'" value="'+conversion.value+'">'+conversion.name+'</option>');
                            //testing output
                            var elem = $("<li>Name: " + conversion.name + " :Value: " + conversion.value + "</li>").appendTo(list);
                        }
                    });
                });
                //$("#testJSON").html(list);
            });
        });
    </script>

编辑: JSON 示例:

{
"angle": [
    {
        "value": "degree",
        "name": "Degree(deg)"
    },
    {
        "value": "radian",
        "name": "Radian(rad)"
    }
]
}
4

0 回答 0