jeudi 14 décembre 2017

jQuery: help needed - data in else in if statement not correct

I have a form which makes some calculations and present the answers on the screen. The calculations are as follows:

  1. We need to calculate the proportion of the music minutes with respect of the length of the show
  2. We multiply that number by predetermined prices
  3. We multiply that number by the # of people
  4. There are 2 discounts to apply according to predetermined values
  5. If the show is for children AND the # of people is over 10,000, there's an extra discount on top of the previous discount

My problem is that the form gives a reduction for children's shows even after choosing "no" on the dropdown. I have reviewed my code several times and can't seem to find what's wrong. Can you please help?

The HTML for the form:

<form method="post" action="URL GOES HERE" class="blue-form">
<div class="field">
    <label class="select " for="field_9579_1">Pris category</label>
    <select class="select " name="field_9579_1">
        <option class="select " value="4"> < 20 music minutes</option>
        <option class="select " value="8">  >= 20 og < 40 music minutes</option>
        <option class="select " value="12">  >= 40 music minutes</option>
    </select>
</div>
<div class="field">
    <label class="text " for="field_9579_2">Music minutes (whole number)</label>
    <input class="text " type="text" name="field_9579_2" id="field_9579_2" value="" />
</div>
<div class="field">
    <label class="text " for="field_9579_3">Show's length (whole number) </label>
    <input class="text " type="text" name="field_9579_3" id="field_9579_3" value="" />
</div>
<div class="field">
    <label class="text " for="field_9579_4">Audience</label>
    <input class="text " type="text" name="field_9579_4" id="field_9579_4" value="" />
</div>
<div class="field">
    <label class="select " for="field_9579_5">Children's show?</label>
    <select class="select " name="field_9579_5">
        <option class="select " value="yes" >Yes</option>
        <option class="select " value="no" >No</option>
    </select>
</div>
<div class="field">
<label class="calculator required " for="field_9579_6">Pris</label>
<div class="calculator_result">
    <span class="calculator_result_text">[click on Calculate]</span> :-
</div>
</div>
<div class="field">
    <input type="submit" value="Calculate" class="blue-form-submit-default" name="blue-form-submit-9579" />
</div>

The jQuery for the calculation:

var priscat = $("select[name="field_9579_1"]").val();
var musicmins = $("#field_9579_2").val();
var shows_length = $("#field_9579_3").val();
var audience = $("#field_9579_4").val();
var for_children = $("select[name="field_9579_5"]").val();

musicamount = musicmins / shows_length;

//start: discount for how big the audience is
if (audience >= 7000 & audience < 10000) {
    discount_audience = 0.85;
} else if (audience >= 10000 & audience < 12000) {
    discount_audience = 0.7;
} else if (audience >= 12000) {
    discount_audience = 0.65;
} else {
    discount_audience = 1;
}
//end: discount for how big the audience is

//start: discount for children's shows
if (for_children = "yes" && audience >= 10000) {
    discount_for_children = 0.66;
} else if (for_children = "ja" && audience < 10000){
    discount_for_children  = 1;
} else if (for_children = "nej") {
    discount_for_children  = 1;
}
//end: discount for children's shows

sum = priscat * musicmins * audience * discount_audience * discount_for_children ;

$(".calculator_result_text").html(sum.toFixed(2));

The else if (for_children="nej") I have changed from a simple else, but that doesn't work either.

Both code snippets are on the same page.

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire