Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
614 views
in Technique[技术] by (71.8m points)

Function not returning in javascript

EDIT: MADE A NEW FUNCTION WITH IF... still doesnt return -.-. I dont understand why.

I am trying to return results of calculating the taxes, total and total + taxes in this function. I want the results to be printed in h3 like the following:

 function taxesrepas(option){
var soustot;
var taxes;
var total;
var soustot;



if (option = getelementbyid("spaghetti")) 
    total += 9.62;
    taxes += 0.67;
    soustot +=8.95;

if (option = getelementbyid("lasagne"))
    total += 10.70;
    taxes += 0.75;
    soustot += 9.95;

if (option = getelementbyid("salade"))
    total +=  6.30;  
    taxes += 0.45;
    soustot += 5.95;
if (option =getelementbyid("escargot"))
    total += 5.32;
    taxes += 0.37;
    soustot += 4.95;




 
    document.getElementById("taxes").innerHTML = taxes;
    document.getElementbyid("total").innerHTML = total;
    document.getElementbyid("soustot").innerHTML = soustot;
        
  }
        
<h3 id="choix"></h3>
<h3 id="taxes" onclick= "taxesrepas(this.value)">taxes:</h3>
<h3 id="soustotal" onclick= "taxesrepas(this.value)">Sous-total:</h3>
<h3 id="total"onclick= "taxesrepas(this.value)">Total:</h3>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The value property is only available on user input elements, not <h3>. And the change event is only triggered when you change the value of an input element, it doesn't apply to <h3>.

You can use textContent to get the string in an <h3> element. And if you want the action to happen when you click on it, use onclick. So they should be like this:

<h3 id="taxes" onclick="taxesrepas(this.textContent)">taxes:</h3>

Also, in the function, you should move the calculations that use taxer and pricer to after the switch statement that sets those variables.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...