Jump to content

Formula result=a*33-86 with javascript?


hariskar

Recommended Posts

IF! you do not want the form to submit by enter or clicking the submit button, which will cause the reloading the page. You need to return false on whatever value are entered! valid or invalid.

function calcResult() {n=document.getElementById('userValue').valuen=parseFloat(n);var userValue=document.forms["hba1c"]["userValue"].value;if (userValue==null || userValue==""){alert("Παρακαλώ καραχωρήστε την τιμή της HbA1c!");}resultEl=(n*33.3-86).toFixed(1);m='mg/dl';d='Εκτιμώμενη μέση τιμή σακχάρου:  ';document.getElementById('resultl').innerHTML=resultEl + " " + m;document.getElementById('table_hba1c').style.display="table";return false;} 

Link to comment
Share on other sites

I'm not sure what the issue is now. Can you please just post your relevant HTML and JS and please tell us specifically what is happening, and what you don't want happening, and you want to happen instead? Using false like we've been saying will work, but only if you implement it correctly.

Link to comment
Share on other sites

Thank you!I would like to have by writing a number and hitting Enter the same behaviour as witing the number and pressing Input type="button" which is onclick="calcResult();How can I hit Enter and start "function calcResult();" (instead of reloading the page like it happenes now)?

You also need to use onclick="return calcResult();", and maybe onsubmit="return calcResult();" in the form element itself.
I use onclick="return calcResult();" for the button and it works, but I cannot use it for <input type="text" id="userValue" size="15">
<form id=hba1c action="" method="post" name="HbA1c_form">Hba1c% <input type="text" id="userValue" size="15"><input type="button" value="Calculate;" onclick="calcResult();"></form> <table id="table_hba1c" style="display:none;"><tr><td width="460"></td><td width="200">Αποτέλεσμα</td></tr><tr><td>Εκτιμώμενη μέση τιμή σακχάρου</td><td id="resultl"></td></tr></table> <script type="text/javascript">function calcResult() {n=document.getElementById('userValue').valuen=parseFloat(n);var userValue=document.forms["hba1c"]["userValue"].value; if (userValue==null || userValue==""){alert("Παρακαλώ καραχωρήστε την τιμή της HbA1c!");return false;} resultEl=(n*33.3-86).toFixed(1);m='mg/dl';d='Εκτιμώμενη μέση τιμή σακχάρου:  ';document.getElementById('resultl').innerHTML=resultEl + " " + m;document.getElementById('table_hba1c').style.display="table"; return false;}</script>

Link to comment
Share on other sites

  • 2 weeks later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...