How to get a number value from an input field?

后端 未结 3 485
粉色の甜心
粉色の甜心 2020-12-15 10:55

I have some issues with calculating some stuff with JS and getting the right values out of the input fields (number). When I use this code it doesn\'t show anything. So what

相关标签:
3条回答
  • 2020-12-15 11:11

    Every id must be converted to integer. Example

    var Malfunctions = parseInt(document.getElementById("Malfunctions").value);
    

    then your ready to go

    0 讨论(0)
  • 2020-12-15 11:14

    You had some mistakes in your HTML, but here is a working JSFiddle: Fiddle

    You you are trying to get elements by their ID, but you don't give them an ID you give them a Name. Also, stop using inline JavaScript calls; it is bad practice.

    function Calculate() {
        var TotalProductionTime = document.getElementById("TotalProductionTime").value;
        var TotalProductionTimeInMinutes = TotalProductionTime * 60;
        var Breaks = document.getElementById("Breaks").value;
        var Malfunctions = document.getElementById("Malfunctions").value;
        var TheoreticalProductionTime = TotalProductionTimeInMinutes - Breaks - Malfunctions;
    
        document.getElementById("test").innerHTML = TheoreticalProductionTime;
    }
    <form id="frm1" action="Calculate.html">
        <table width="350px" border="1px">
            <tr>
                <th colspan="2">Availability</th>
            </tr>
            <tr>
                <td>Total Production Time</td>
                <td>
                    <input type="number" id="TotalProductionTime" placeholder="">hours</td>
            </tr>
            <tr>
                <td>Breaks</td>
                <td>
                    <input type="number" id="Breaks" placeholder="">minutes</td>
            </tr>
            <tr>
                <td>Malfunctions</td>
                <td>
                    <input type="number" id="Malfunctions" placeholder="">minutes</td>
            </tr>
            <tr>
                <td>Theoretical production time:</td>
                <td>
                    <p id="test"></p>
                </td>
            </tr>
        </table>
        <input type="button" onclick="Calculate()" value="calculate">
    </form>

    0 讨论(0)
  • 2020-12-15 11:25

    You've got two problems here. One obvious is that you try to get a reference to the form inputs by id, but didn't give them any (you gave them a name). To fix, either change the name attribute to an id, or use the form-specific way to reference them, e.g.:

    var TotalProductionTime = document.forms.frm1.TotalProductionTime
    

    Second problem is more vicious and has to do with the scope of execution of what you put in onclick attributes. You see, your button is named "Calculate" just like your function, and in the context of the onclick attribute, its parent form is used to resolve identifiers before the global scope. So instead of calling the function named Calculate, you're trying to call the button itself. Fix that by giving them different names, referencing window.Calculate explicitly, or much better, define your event handler in JavaScript instead of using the HTML attribute:

    document.forms.frm1.Calculate.onclick=Calculate
    
    0 讨论(0)
提交回复
热议问题