简体   繁体   中英

Javascript calculation not working.

I have one page and two separate script to calculate value but if I repeat two time Javascript in one page it is not working how can I do this? Please help in this issue. Thanks in advance.

<script type="text/javascript"><!--
function updatesum() {
    var total = (document.form.daysone.value -0)*(document.form.daystwo.value -0)* (document.form.amount.value -0)/ (document.form.year.value -0)/ (document.form.month.value -0);
    document.form.total.value = total.toFixed(3);
}
//--></script>
 <form name="form" action="enter.php" method="post" id="searchform">
     <p>
       <label class="field">15 Days</label>
       <input  name="daysone" type="text" value="15"  onChange="updatesum()" />
     </p> 
          <p>
       <label class="field">686 Days</label>
       <input  name="daystwo" type="text" value="686"  onChange="updatesum()" />
     </p> 
              <p>
       <label class="field">Amount </label>
       <input  name="amount" type="text" value=""  onChange="updatesum()" />
     </p>
              <p>
       <label class="field">year </label>
       <input  name="year" type="text" value="365"  onChange="updatesum()" />
     </p>
              <p>
       <label class="field">month </label>
       <input  name="month" type="text" value="30"  onChange="updatesum()" />
     </p>
      <p>
        <label class="field" >Total </label>
        <input name="total" type="text" value="" />
     </p>





      <strong>next one</strong>


<script type="text/javascript"><!--
function updatesum() {
    var total1 = (document.form1.days1.value -0)*(document.form1.days1.value -0)* (document.form1.amount1.value -0)/ ;
    document.form1.total1.value = total1.toFixed(3);
}
//--></script>
 <form name="form1" action="enter.php" method="post" id="searchform">
     <p>
       <label class="field">15 Days</label>
       <input  name="days1" type="text" value="15"  onChange="updatesum()" />
     </p> 
          <p>
       <label class="field">686 Days</label>
       <input  name="days2" type="text" value="686"  onChange="updatesum()" />
     </p> 
              <p>
       <label class="field">Amount </label>
       <input  name="amount1" type="text" value=""  onChange="updatesum()" />
     </p>

      <p>
        <label class="field" >Total </label>
        <input name="total1" type="text" value="" />
     </p>

Try to use this function, and replace your with this one.

<script type="text/javascript">
function updatesum() {


    var total = (document.form.daysone.value -0)*(document.form.daystwo.value -0)* (document.form.amount.value -0)/ (document.form.year.value -0)/ (document.form.month.value -0);
    document.form.total.value = total.toFixed(3);
}
</script>
 <form name="form" action="enter.php" method="post" id="searchform">
     <p>
       <label class="field">15 Days</label>
       <input  name="daysone" type="text" value="15"  onChange="updatesum()" />
     </p>
          <p>
       <label class="field">686 Days</label>
       <input  name="daystwo" type="text" value="686"  onChange="updatesum()" />
     </p>
              <p>
       <label class="field">Amount </label>
       <input  name="amount" type="text" value=""  onChange="updatesum()" />
     </p>
              <p>
       <label class="field">year </label>
       <input  name="year" type="text" value="365"  onChange="updatesum()" />
     </p>
              <p>
       <label class="field">month </label>
       <input  name="month" type="text" value="30"  onChange="updatesum()" />
     </p>
      <p>
        <label class="field" >Total </label>
        <input name="total" type="text" value="" />
     </p>

I think the same name is creating a problem here, Is it necessary to have the same name for both the functions?

Please try to change the name from UpdateSum in second one to something else.

The issue is with the duplicate function name.

Since, both the functions have a different calculation, it is better you name them differently and update your html accordingly.

There is another way in which you can do this, but you'll have to pass a parameter to the function which will differentiate between the two forms.

function updatesum(formName) {
    var total, total1;
    if (formName == 'form') {
        total = (document.form.daysone.value - 0) * (document.form.daystwo.value - 0) * (document.form.amount.value - 0) / (document.form.year.value - 0) / (document.form.month.value - 0);
        document.form.total.value = total.toFixed(3);
    } else if (formName == 'form1') {
        total1 = (document.form1.days1.value - 0) * (document.form1.days1.value - 0) * (document.form1.amount1.value - 0);
        document.form1.total1.value = total1.toFixed(3);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM