-->

javascript function says that is undefined

2019-08-24 02:19发布

问题:

all I've written a function in order to calculate many things,

so I've put the value of my vars in input of type hidden, so we have this code:

 <input type="hidden" id="p1" value="5000.00">
 <input type="hidden" id="p2" value="5000.01">
 <input type="hidden" id="p3" value="8000.00">
 <input type="hidden" id="p4" value="8000.01">
 <input type="hidden" id="t1" value="15.0">
 <input type="hidden" id="t2" value="12.0">
 <input type="hidden" id="t3" value="6.0">
 <input type="hidden" id="versement" value="12554.35">
 <input type="hidden" id="type" value="d">

Moreover, I've have the following code, first an input in order to write the amount to be calculated:

and the function on the top:

so we have:

 <script language="text/javascript">
 function hono(p1,p2,p3,p4,t1,t2,t3,type,versement,montantacompte)
 {
     var p1             = document.getElementById("p1").value;
     var p2             = document.getElementById("p2").value;
     var p3             = document.getElementById("p3").value;
     var p4             = document.getElementById("p4").value;
     var t1             = document.getElementById("t1").value;
     var t2             = document.getElementById("t2").value;
     var t3             = document.getElementById("t3").value;
     var type           = document.getElementById("type").value;    
     var versement      = document.getElementById("versement").value;
     var montantacompte = document.getElementById("montantacompte").value;

     if (type== "d")
        {
            if(versement== 0 && montantacompte <= p1)
            {
                reste1= montantacompte;
                pc= t1/100;
                taux1= pc+1;
                h1=((reste1*taux1)+reste1);
                honoraires=h1;
                document.getElementById('taux-1').value=taux1;
                document.getElementById('palier1').value=h1;
                document.getElementById('sommehonoraires').value=honoraires;
            }
            else if(versement < p1 && (versement + acompte) <= p1)
            {
                reste1= montantacompte;
                pc= t1/100;
                taux1= pc+1;
                h1= (reste1*taux1)-reste1;
                honoraires= h1;
                document.getElementById('taux-1').value=taux1;
                document.getElementById('palier1').value=h1;
                document.getElementById('sommehonoraires').value=honoraires;
            }
            else if(versement <= p1 && (versement+montantacompte)>= p2 && (versement+montantacompte)<=p3)
            {
                reste1=p1-versement;
                pc=t1/100;
                taux1=pc+1;
                h1=(reste1*taux1)-reste1;
                reste2=montantacompte-reste1;
                pc2=t2/100;
                taux2=pc2+1;
                h2=(reste2*taux2)-reste2;
                honoraires=h1+h2;
                document.getElementById('taux-1').value=taux1;
                document.getElementById('palier1').value=h1;
                document.getElementById('taux-2').value=taux2;
                document.getElementById('palier-2').value=h2;
                document.getElementById('sommehonoraires').value=honoraires;
            }
            else if (versement==0 && montantacompte>=p2 && montantacompte <=p3)
            {
                reste2=montantacompte-p1;
                reste1=p1;
                pc=(t1/100);
                taux1=pc+1;
                h1=((reste1*taux1)-reste1);
                pc2=(t2/100);
                taux2=pc2+1;
                h2=((reste2*taux2)-reste2);
                honoraires=(h1+h2);
                document.getElementById('taux-1').value=taux1;
                document.getElementById('palier1').value=h1;
                document.getElementById('taux-2').value=taux2;
                document.getElementById('palier2').value=h2;
                document.getElementById('sommehonoraires').value=honoraires;
            }
            else if(versement<=p3 && versement>=p2 && (versement+montantacompte)>=p2 && (versement+montantacompte)<=$3)
            {
            reste1=montantacompte;
            pc=($2/100);
            taux1=pc+1;
            h1=((reste1*taux1)-reste1);
            honoraires=h1;  
            document.getElementById('taux-1').value=taux1;
            document.getElementById('palier1').value=h1;
            document.getElementById('sommehonoraires').value=honoraires;
            }
            else if(versement>=p2 && versement<=p3 && (versement+montantacompte)>=p4)
            {
            reste1=p4-versement;
            pc=(t2/100);
            taux2=pc+1;
            h2=((reste1*taux2)-reste1);
            reste2=montantacompte-reste1;
            pc2=(t3/100);
            taux3=pc2+1;
            h3=((reste2*taux3)-reste2);
            honoraires=(h2+h3);
            document.getElementById('taux-3').value=taux3;
            document.getElementById('palier3').value=h3;
            document.getElementById('taux-2').value=taux2;
            document.getElementById('palier2').value=h2;
            document.getElementById('sommehonoraires').value=honoraires;
        }
        else if(versement==0 && montantacompte>=p4)
        {
            reste1=p1;
            reste2=p3-p2;
            pc=(t1/100);
            taux1=pc+1;
            h1=((reste1*taux1)-reste1);
            pc2=(t2/100);
            taux2=pc2+1;
            h2=((reste2*taux2)-reste2);
            reste3=(montantacompte-reste1-reste2);
            pc3=(t3/100);
            taux3=pc3+1;
            h3=((reste3*$taux3)-reste3);
            honoraires=(h1+h2+h3);
            document.getElementById('taux-1').value=taux1;
            document.getElementById('palier1').value=h1;
            document.getElementById('taux-3').value=taux3;
            document.getElementById('palier3').value=h3;
            document.getElementById('taux-2').value=taux2;
            document.getElementById('palier2').value=h2;
            document.getElementById('sommehonoraires').value=honoraires;
        }
    else if(versement>=p4)
        {
            reste3=montantacompte;
            pc3=($3/100);
            taux3=pc3+1;
            h3=((reste3*taux3)-reste3);
            honoraires=(h3);
            document.getElementById('taux-3').value=taux3;
            document.getElementById('palier3').value=h3;
            document.getElementById('sommehonoraires').value=honoraires;
        }
    }
    if (type=="l")
    {
    pc1=(t1/100);
    taux1=pc1+1;
    honoraires=montantacompte*taux1;
    document.getElementById('taux-1').value=taux1;
    document.getElementById('palier1').value=h1;
    document.getElementById('sommehonoraires').value=honoraires;
    }
 }

 </script><input type="text" name="montantacompte" id="montantacompte" onKeyUp="hono(p1,p2,p3,p4,t1,t2,t3,type,versement,montantacompte)"  /> 

And in the console it says to me that the function is not defined, or it is, because i've written it... All is in the body.

We have this in the console:

Uncaught ReferenceError: hono is not defined test.php:166 onkeyup

and the line 166 is where there is the input type text after the function.

sorry for my bad English,

If you need more precisions, just ask me,

回答1:

language="text/javascript" is wrong it should be language="JavaScript" but thats old/outdated/obsolete/depreciated so use type="text/javascript" instead



回答2:

At first glance, I noticed you have your script attributes wrong.

 <script language="text/javascript">

should be:

 <script type="text/javascript">