javascript - How do you clear variable values held in a switch function? -
javascript - How do you clear variable values held in a switch function? -
i have been working on project school several weeks , have came here several times point me in right direction, give thanks that. issue (and instructor) struggling over.
i have create order form (...yep) user must select computer case, monitor, , printer. image must appear next selection , cost must update. if checks order submits, if not alert asking finish form. have alternative reset or clear form. work wonderfully except clear function. fields reset including price, individual values still beingness held. if pick first alternative on sections total $1000, after clear total field shows zero. then, if pick alternative grouping a, values still totaling grouping b , c if nil cleared.
hope explained clearly, here's have:
<html> <head> <script> function dosubmit() { if (validatetext() == false) { alert("please finish client information"); return; } if (validateradio() == false) { alert("please select case") return; } if (validateradio1() == false) { alert("please select monitor") return; } if (validateradio2() == false) { alert("please select printer") return; } alert("order accepted. give thanks you."); return; } function validatetext() { var client = document.form4.customer.value; if (customer.length == 0) homecoming false; var address1 = document.form4.address1.value; if (address1.length == 0) homecoming false; var city = document.form4.city.value; if (city.length == 0) homecoming false; var phone = document.form4.phone.value; if (phone.length == 0) homecoming false; var email = document.form4.email.value; if (email.length == 0) homecoming false; homecoming true; } function validateradio() { if (document.form1.case[0].checked) homecoming true; if (document.form1.case[1].checked) homecoming true; if (document.form1.case[2].checked) homecoming true; homecoming false; } function validateradio1() { if (document.form2.screen[0].checked) homecoming true; if (document.form2.screen[1].checked) homecoming true; if (document.form2.screen[2].checked) homecoming true; if (document.form2.screen[3].checked) homecoming true; homecoming false; } function validateradio2() { if (document.form3.printer[0].checked) homecoming true; if (document.form3.printer[1].checked) homecoming true; if (document.form3.printer[2].checked) homecoming true; homecoming false; } function doclear() { document.form4.systotal.value = "0.00"; document.form4.customer.value = ""; document.form4.address1.value = ""; document.form4.address2.value = ""; document.form4.city.value = ""; document.form4.state.value = ""; document.form4.zip.value = ""; document.form4.phone.value = ""; document.form4.email.value = ""; document.form1.case[0].checked = false; document.form1.case[1].checked = false; document.form1.case[2].checked = false; document.form2.screen[0].checked = false; document.form2.screen[1].checked = false; document.form2.screen[2].checked = false; document.form2.screen[3].checked = false; document.form3.printer[0].checked = false; document.form3.printer[1].checked = false; document.form3.printer[2].checked = false; document.form3.printer[3].checked = false; formpics(11); return; } var computercase = 0; var printer = 0; var monitor = 0; var total = 0; function formpics(radios) { switch(radios) { case 1: document.getelementbyid("pics").innerhtml = "<img src='c1_game.jpg'>"; computercase = 600.00; total = computercase + printer + monitor; document.form4.systotal.value = total; break; case 2: document.getelementbyid("pics").innerhtml = "<img src='c2_home.jpg'>"; computercase = 500.00; total = computercase + printer + monitor; document.form4.systotal.value = total; break; case 3: document.getelementbyid("pics").innerhtml = "<img src='c3_mini.jpg'>"; computercase = 400.00; total = computercase + printer + monitor; document.form4.systotal.value = total; break; case 4: document.getelementbyid("pics2").innerhtml = "<img src='s1_27.jpg'>"; monitor = 350.00; total = computercase + printer + monitor; document.form4.systotal.value = total; break; case 5: document.getelementbyid("pics2").innerhtml = "<img src='s2_19.jpg'>"; monitor = 250.00; total = computercase + printer + monitor; document.form4.systotal.value = total; break; case 6: document.getelementbyid("pics2").innerhtml = "<img src='s3_17.jpg'>"; monitor = 150.00; total = computercase + printer + monitor; document.form4.systotal.value = total; break; case 7: document.getelementbyid("pics2").innerhtml = "<img src='s4_proj.jpg'>"; monitor = 650.00; total = computercase + printer + monitor; document.form4.systotal.value = total; break; case 8: document.getelementbyid("pics3").innerhtml = "<img src='p1_ink.jpg'>"; printer = 50.00; total = computercase + printer + monitor; document.form4.systotal.value = total; break; case 9: document.getelementbyid("pics3").innerhtml = "<img src='p2_laser.jpg'>"; printer = 80.00; total = computercase + printer + monitor; document.form4.systotal.value = total; break; case 10: document.getelementbyid("pics3").innerhtml = "<img src='p3_color.jpg'>"; printer = 100.00; total = computercase + printer + monitor; document.form4.systotal.value = total; break; case 11: computercase = 0; printer = 0; monitor = 0; total = 0; break; } } function totalclear() { if(printer > 0) printer = 0; homecoming printer; } </script> <style> table{ width:800px} #pics{ float: right;} #pics2{ float: right;} #pics3{ float: right;} </style> </head> <body> <h1 align="center">new scheme order form</h1> <table border="1" cellpadding="5" align="center"> <tr> <td> <form name="form1" align="left"> <h3>choose case:</h3><div id='pics'></div> <input name="case" type="radio" onclick='formpics(1)'>gaming behemoth ($600.00)<br/> <input name="case" type="radio" onclick='formpics(2)'>basic home computing ($500.00) <br/> <input name="case" type="radio" onclick='formpics(3)'>mini amusement center ($400.00)<br/> </form> <br/> <form name="form2"> <h3>choose monitor:</h3><div id='pics2'></div> <input name="screen" type="radio" onclick='formpics(4)'>27" uhd led ($350.00)<br/> <input name="screen" type="radio" onclick='formpics(5)'>19"hd lcd ($250.00)<br/> <input name="screen" type="radio" onclick='formpics(6)'>17"hd lcd ($150.00)<br/> <input name="screen" type="radio" onclick='formpics(7)'>hd laser projector ($650.00) <br/> </form> <br/> <form name="form3"> <h3>choose printer:</h3><div id='pics3'></div> <input name="printer" type="radio" onclick='formpics(8)'>inkjet printer ($50.00) <br/> <input name="printer" type="radio" onclick='formpics(9)'>laser printer ($80.00)<br/> <input name="printer" type="radio" onclick='formpics(10)'>color laser printer ($100.00)<br/> </form> </td> <form name="form4"> <td> <h3 align="left">system total: $ <input type="text" name="systotal" size="10" readonly = "readonly" value = "0.00" /></h3> <hr style="width:100%" /> <h3 align="left">customer information:</h3> <b>full name:</b><br/> <input name="customer" size="45" type="text"><br/> <b>address:</b><br/> <input name="address1" size="45" type="text"><br/> <input name="address2" size="45" type="text"><br/> <b>city, state, , zip:</b><br/> <input name="city" size="15 type="text"> <input name="state" size="2" type="text"> <input name="zip" size="5" type="text"><br/> <b>phone:</b><br/> <input name="phone" size="40" type="text"><br/> <b>email:</b><br/> <input name="email" size="40" type="text"><br/> </form> <hr style="width:100%" /> <br/> <input type="button" value="submit order" onclick="dosubmit()"> <input type="button" value="reset order" onclick="doclear()"> </td> </tr> </table> </body> </html>
you have 3 printers, seek clear four. code in function doclear
gives error message:
typeerror: document.form3.printer[3] undefined
as code crashes there, phone call formpics(11)
never executed, variables never cleared.
javascript switch-statement
Comments
Post a Comment