0

我是 html 和 js 的新手。我使用以下脚本创建了一个表单。我有一个计算时间的事件处理程序。我似乎无法获得要计算的表格。如果你们能指出我正确的方向,那就太好了。谢谢

<script>
function getWaterSystem() {
    var waterSystem;
    var selectedSystem = 0;

    for (var i = 1; i <= 4; i++) {
        waterSystem = document.getElementById("system + i");
        if (waterSystem.checked == true) {
            waterSystem = waterSystem(i).value;
        }
    }
    return waterSystem;
}
function largePlant() {
    var checkbox;

    checkbox = document.getElementById("large");
    if (checkbox(i).checked) {
        checkbox = 1.5;
    }
    else {
        checkbox = "";
    }
    return checkbox;
}
function getSoilType() {
    var soilType;
    var selectedSoil = 0;

    for (var i = 1; i <= 3; i++) {
        soilType = document.getElementById("soil + i");
        if (soilType[i].checked) {
            soilType = soilType[i].value;
        }
    }
    return soilType;
}
function calculateTime() {
    var waterTime = getWaterSystem() * getSoilType() * largePlant();
    alert("The recommended watering time is " + waterTime);
}
</script>
4

2 回答 2

0

我做了一些调整,检查一下,很简单:document.getElementById("check1").checked

<script>
    function getWaterSystem() {
        var waterSystem=0;
        var selectedSystem = 0;

        for (var i = 1; i <= 4; i++) {

            if (document.getElementById("system" + i).checked) {
                reutrn waterSystem = document.getElementById("system" + i).value;
            }
        }
        return waterSystem;
    }
    function largePlant() {
        var checkbox=0;
        if (document.getElementById("large").checked) {
            checkbox = 1.5;
        }
        else {
            checkbox = 0;
        }
        return checkbox;
    }
    function getSoilType() {
        var soilType=0;
        var selectedSoil = 0;

        for (var i = 1; i <= 3; i++) {
            soilType = document.getElementById("soil" + i);
            if (document.getElementById("soil" + i). checked) {
                soilType = document.getElementById("soil" + i).value;
                return soilType;
            }
        }
        return soilType;
    }
    function calculateTime() {
        var waterTime = getWaterSystem() * getSoilType() * largePlant();
        alert("The recommended watering time is " + waterTime);
    }
    </script>
于 2013-09-25T11:18:19.877 回答
0

它可能与这两行有关:

waterSystem = document.getElementById("system + i");

soilType = document.getElementById("soil + i");

如果您想定位 #systemX 或 #soilX 的 id,其中 X 是您的数字,我 var。你应该这样写:

waterSystem = document.getElementById("system" + i);

soilType = document.getElementById("soil" + i);
于 2013-09-25T11:02:02.117 回答