since it should load data from form fields, but m1 is not an access key for the form field. and m1 is not a global variable, so it fails. create 2 global variables and let the forms m1 and m2 keep their values ββthere when changing.
your original script fails because your function evaluates m1 and m2, but they are destroyed when the function ends, because they are not a global scope
function displayResult(thisElement) { thisElement.value = eval(thisElement.value); if(!(thisElement.value>0)) { thisElement.value=0; } } m1=0; m2=0; <input id="calcFormula" name="calculate" size="40" /> <input type="submit" value="Calculate" onclick="displayResult(this.form.calc ulate);" /> <input name="m1" id="m1" onchange="m1=this.value" size="12" value="0" /> <input name="m2" id="m2" size="12" onchange="m2=this.value;" value="0" />
Why your source code failed:
Step 1. M1 is stored in the form. step 2. Onchange event is fired step 3. Function mem(this) is called step 4. Entering scope of function mem step 5. Inserting this.name into string step 6. Inserting this.value into string Step 7. evalling string step 7.1 found code m1=1 step 7.1.1 check window object for variable named m1 step 7.1.1.1 failed. Create local variable for function mem called m1 step 7.1.1.2 assign value 1 to scope variable m1 step 7.1.1.3 exit eval step 7.1.1.4 exit function mem(this) step 7.1.1.5 check for scoped variables step 7.1.1.6 scoped variable found m1 step 7.1.1.7 destroy scoped variable and free up memory step 7.1.2.2 passed. retrieve pointer to window object variable m1 step 7.1.2.3 assign value 1 to window.m1 step 7.1.2.4 exit eval step 7.1.2.5 exit function mem(this) step 7.1.2.6 check for scoped variables step 7.1.2.7 none found, resume other tasks.
source share