✔ 最佳答案
Fisrt, insert:
1.a form with id="form1" :
--------------------------------> input type="form" id="form1"
2.testfields with id="num1" and "num2":
--------------------------------> input type="text" id="num1" ; input type="text" id="num2"
3.a button:
--------------------------------> input type="button" value="Add"
4.a reset button
--------------------------------> input type="reset" value="Reset"
-----------------------------------------------------------------------------------------------------
Then, add the funtion in script in head:
function AddNum(a,b) {
if (!isNaN(parseFloat(a)) && !isNaN(parseFloat(b))) {
//check if a and b are numbers
alert(parseFloat(a) + parseFloat(b));
//parseFloat(a): change a into a number
} else {
alert("You must enter numbers in both textfields!");
}
}
-----------------------------------------------------------------------------------------------------
Finally, in the tag of the button, add:
onClick="AddNum(document.getElementById( 'num1' ).value, document.getElementById( 'num2' ).value) "
That is!
2007-07-29 11:51:41 補充:
Sorry, the symbols becomes something no one knows." = " and; Finally, in the tag of the button, add:onClick= "AddNum(document.getElementById('num1').value, document.getElementById("num2').value)"
2007-07-29 13:14:42 補充:
See this template:
http://rapidshare.com/files/45681852/Untitled-1.html