Help me this question Who knoe how to do?/

2007-07-29 6:41 pm
Write the html codes to output the form below. Add the 2 numbers and display the result when the add button is clicked Clear all fields when the clear button is clicked.
Verify first num at first fields and second n um at second fields user key in number otherwise a mesage Invalid number displayed.

回答 (1)

2007-07-29 7:49 pm
✔ 最佳答案
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


收錄日期: 2021-04-13 00:53:35
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20070729000051KK01224

檢視 Wayback Machine 備份