How to get a number value from an input field?

后端 未结 3 497
粉色の甜心
粉色の甜心 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: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
    

提交回复
热议问题