我如何获得输出中显示的值,并将其设置为变量?

时间:2018-04-12 13:23:05

标签: javascript html

如何获取输出中显示的值,并将其设置为变量,以便我可以将其乘以另一个变量?

let calculate = () => {
    let box1_selection = getValueById("pattern");
    let box2_selection = getValueById("thick");

    if (box1_selection == "" || box2_selection == "") {
        document.getElementById("output").innerHTML = "Please select both values";
    } else {
        let value = "not specified";

        /*if(box2_selection == 0 && box3_selection == 0 {
            value = "$27.00";
        } else if(box2_selection == 0 && box3_selection == 1) {
            value = "$17.00";
        }*/
        var lolm = getPrice(box1_selection, box2_selection);

        document.getElementById("output").innerHTML = lolm;
    }
}

var getValueById = (id) => {
    var selection = document.getElementById(id);
    return selection.options[selection.selectedIndex].value;
}

var getPrice = (value_1, value_2) => {
    // price_data is a 3 dimensional array.
    var price_data = [
        [
            [8.00],
            [13.50]
        ],
        [
            [20.80],
            [22.05]
        ],
        [
            [53.10],
            [99]
        ],
        [
            [20.20],
            [99]
        ],
        [
            [24.00],
            [99]
        ],
        [
            [99],
            [99]
        ],
        [
            [15.23],
            [99]
        ]
    ];
    return price_data[value_1][value_2];

    var roundedvalue1 = Math.ceil(document.getElementById('box3').value);
    var roundedvalue2 = Math.ceil(document.getElementById('box4').value);
    if (roundedvalue1 % 2 != 0) {
        roundedvalue1 += 1;
    }

    if (roundedvalue2 % 2 != 0) {
        roundedvalue2 += 1;
    }
    var result = document.getElementById('result');
    var rounded1plus2 = (roundedvalue1 + 2)
    var rounded2plus2 = (roundedvalue2 + 2)
    var squareFoot = (rounded1plus2 * rounded2plus2 / 144)
    var myResult = +lolm;
    result.value = myResult;
}
<select id="pattern" onchange="calculate()">
        <option value="">Select Glass Pattern</option>
        <option value="0">Clear</option>
        <option value="1">Starphire</option>
        <option value="2">Bamboo</option>
        <option value="3">Rain</option>
        <option value="4">Satin Etch</option>
        <option value="5">MasterCarre</option>
        <option value="6">Rolled Glue Chip</option>
    </select>
<select id="thick" onchange="calculate()">
        <option value="">Select Glass Thickness</option>
        <option value="0">3/8 (10mm)</option>
        <option value="1">1/2 (12mm)</option>
    </select>
<td><input id="box3" type="text" oninput="calculate()" placeholder="Whole #'s" maxlength="3" size="4" /></td>
<td><input id="box4" type="text" oninput="calculate()" placeholder="Whole #'s" maxlength="3" size="4" /></td>
<td><b>$</b><input id="result" maxlength="4" size="4" />Flat</td>
<br>
<div id="output">Please select both values</div>
<div>

</div>

基本上我要做的是抓取网页上显示的输出作为实际文本,set等于随选择而变化的变量,我可以乘以其他部分的其他变量脚本。

1 个答案:

答案 0 :(得分:0)

我没有完全理解你想要的东西。这是我的解决方案,如果有帮助的话。

您已经在此行中获得了输出值

var lolm = getPrice(box1_selection, box2_selection);`

它已设置为lolm。你可以随意操纵。

您已在lolm箭头功能中将calculate声明为var lolm。所以它的函数作用域,如果你想在函数外部访问它(在其他函数中使用它)全局声明变量lolm(在函数之外)。