รบกวนผู้เชี่ยวชาญ ช่วยมือใหม่เรื่องการเขียนโค๊ดคำนวณทีคับ

ปัญหา การเขียน JavaScript เครื่องมือ

Moderator: phpbb, mindphp

ตอบกลับโพส
bujoher
phpBBThailand Newbie
phpBBThailand Newbie
โพสต์: 1
ลงทะเบียนเมื่อ: 26 มี.ค. 2009, 18:26

รบกวนผู้เชี่ยวชาญ ช่วยมือใหม่เรื่องการเขียนโค๊ดคำนวณทีคับ

โพสต์ โดย bujoher »

<html>
<script>
function add(op,x,y)
{

if(op=="+")
txtOut.value = parseInt(x) + parseInt(y)
else if(op=="-")
txtOut.value = parseInt(x) - parseInt(y)
else if(op=="*")
txtOut.value = x*y
else if(op=="/")
txtOut.value = x/y
else if(op=="^")
txtOut.value = Math.pow(parseInt(x),parseInt(y))
else if(op=="%")
txtOut.value = parseInt(x) % parseInt(y)
else
txtOut.value = "Invalid Operator, Please Enter Now"

}

function clears()
{

txt1.value=""
txt2.value=""
txtOut.value=""
txt1.focus()
}

</script>

<body>

Number1<input type=text name=txt1 value=""><br>
Operator <input type=button name="plus" value="+" onclick="add('+')">
<input type=button name="minus" value="-" onclick="add('-')">
<input type=button name="mul" value="*" onclick="add('*')">
<input type=button name="div" value="/" onclick="add('/')">
<input type=button name="pow" value="^" onclick="add('^')">
<input type=button name="mod" value="%" onclick="add('%')"><br>
Number2<input type=text name=txt2 value="" ><br>
Output<input type=text name=txtOut value=""><br>

<input type=button name=btn1 value="Caculate" onclick="add(txt1.value,txt2.value)">
<input type=button name=btn2 value="Clear" onclick="clears()">
</body>

</html>
-----------------------------------------------------------------------------------------------------------
ผมพึ่งหัดเขียน javascript อ่ะคับ จากโค๊ดนี้ผมอยากจาให้ user ใส่ตัวเลข ที่ txt1 แล้วเลือกปุ่มเครื่องหมาย
ที่จะคำนวนเช่น+,-,*,/,^,% แล้วใส่ตัวเลขอีกตัวที่ txt2 พอจาคำนวนก็กดที่ปุ่ม calculate
เพื่อให้แสดงค่าออกมาใน txtOut ปรากฎว่า!! ทำยังไง มันก็ไม่ยอมคำนวนให้คับงงมาหลาย ชม. แล้ว T T
รบกวนผู้รู้ช่วยแนะนำผมทีคับ ขอบพระคุณอย่างสูงครับ :)
Doesystem
phpBBThailand Newbie
phpBBThailand Newbie
โพสต์: 8
ลงทะเบียนเมื่อ: 06 ก.ย. 2012, 01:00
ที่อยู่: Patum
ติดต่อ:

Re: รบกวนผู้เชี่ยวชาญ ช่วยมือใหม่เรื่องการเขียนโค๊ดคำนวณทีค

โพสต์ โดย Doesystem »

ตัวอย่างการทำ หน้าเว็บนี้ครับ
http://www.doesystem.com/ec836f3e2ee0b634e0537ceb605f97bf/การสร้างเครื่องคำนวณง่าย-ๆ-โดย-JavaScript.htm
http://www.doesystem.com/" onclick="window.open(this.href);return false;
ตอบกลับโพส

ย้อนกลับไปยัง

ผู้ใช้งานขณะนี้

กำลังดูบอร์ดนี้: 78 และ บุคคลทั่วไป 0 ท่าน