issue with change function on select option in Jquery

Rashad

I have a little select form. When I change options using JQUERY, new suitable price appears inside .prices. AN old price is 100 $. if user is student or pupil, I want make him discount -10 $. So when user choose option student or pupil price is changed from 100 to 90$. But when user choose option again to pupil it must not be change the price, but it changes it to 80 $. Again when choose option student, price was changed to 70. I exactly want that if user choose option: student or pupil, there will be -10 $ discount.

you can ask why I used each function? The option: other is selected and first time page shows price 110$. each function fixed this problem. I have also JS fiddle, you can check it. Sorry for my English Language.

DEMO

HTML

<select name="status" id="status">
    <option value="1">Student</option>
    <option value="2">Pupil</option>
    <option value="3" selected>Other</option>
</select>

<div class="price">
    <span class="prices">100</span> $
</div>

JQUERY

$( "#status" ).change(function () {

    var price2 = ($(".prices").text());

    $('.prices').text('');

    var value = $( "#status option:selected" ).val();

    if(value=="3")
    {     
        new_price2 = +price2 +  +10;       
        $('.prices').append(new_price2);
    }

    else
    {
        new_price2 = price2 - 10;     
        $('.prices').append(new_price2);
    }
})
.change();

$('#status option').each(function() {

 if(this.selected)
  {
    var price2 = ($(".prices").html());
    $('.prices').html('');

    new_price2 = price2 - 10;
    $('.prices').append(new_price2);
  }

});
Rory McCrossan

You only need the change handler in your jQuery code. Also, you need to keep a static base price value so that you can do all calculations on that instead of keeping a running total of all changes. Try this:

$("#status").change(function () {
    var price2 = $(this).data('base-price');
    if ($("option:selected", this).val() == "3") {
        new_price2 = price2;
    } else {
        new_price2 = price2 - 10;
    }
    $('.prices').text(new_price2);
}).change();

Example fiddle

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Issue in select option in Jquery mobile

From Dev

javascript - select option jquery issue

From Dev

jquery event issue with select option

From Dev

Issue on change jquery function

From Dev

jQuery change a textbox with a <select> option

From Dev

Call a function on change of a select option in angularjs

From Java

jquery select change event get selected option

From Dev

jquery change event not working in select option

From Dev

Change the existing value in a select/option dropdown with JQuery

From Dev

jquery loop through select tags on option change

From Dev

jQuery .clone() of a <select>: change the selected option

From Dev

jQuery When I select an Option to call a function

From Dev

Jquery function to split option list into hierarchical select

From Dev

Converting a JavaScript function to jQuery - Select option list

From Dev

Select (trigger change) the dynamically added option of a select dropdown in Javascript/jQuery

From Dev

Issue in Chrome browser when select's option is removed with jQuery

From Dev

jQuery - contains issue when trying to select option menu

From Dev

JQuery change select option when text input like value

From Dev

select2 - How to change the value to the first option of the list with jQuery?

From Dev

Jquery set select option by .val() does not fire on('change', )

From Dev

Jquery how to change color of one word in select <option> box

From Dev

jQuery event on select elements when an option is selected (NOT .change)

From Dev

how to get the value of option values on select change in jquery

From Dev

Hide a div upon option change in select dropdown in HTML using jQuery

From Dev

jquery select change event when get selected option

From Dev

Jquery how to change color of one word in select <option> box

From Dev

jQuery .change() doesn't react on select with 1 option

From Dev

select2 - How to change the value to the first option of the list with jQuery?

From Dev

Detect change of option values of an select field using jquery

Related Related

  1. 1

    Issue in select option in Jquery mobile

  2. 2

    javascript - select option jquery issue

  3. 3

    jquery event issue with select option

  4. 4

    Issue on change jquery function

  5. 5

    jQuery change a textbox with a <select> option

  6. 6

    Call a function on change of a select option in angularjs

  7. 7

    jquery select change event get selected option

  8. 8

    jquery change event not working in select option

  9. 9

    Change the existing value in a select/option dropdown with JQuery

  10. 10

    jquery loop through select tags on option change

  11. 11

    jQuery .clone() of a <select>: change the selected option

  12. 12

    jQuery When I select an Option to call a function

  13. 13

    Jquery function to split option list into hierarchical select

  14. 14

    Converting a JavaScript function to jQuery - Select option list

  15. 15

    Select (trigger change) the dynamically added option of a select dropdown in Javascript/jQuery

  16. 16

    Issue in Chrome browser when select's option is removed with jQuery

  17. 17

    jQuery - contains issue when trying to select option menu

  18. 18

    JQuery change select option when text input like value

  19. 19

    select2 - How to change the value to the first option of the list with jQuery?

  20. 20

    Jquery set select option by .val() does not fire on('change', )

  21. 21

    Jquery how to change color of one word in select <option> box

  22. 22

    jQuery event on select elements when an option is selected (NOT .change)

  23. 23

    how to get the value of option values on select change in jquery

  24. 24

    Hide a div upon option change in select dropdown in HTML using jQuery

  25. 25

    jquery select change event when get selected option

  26. 26

    Jquery how to change color of one word in select <option> box

  27. 27

    jQuery .change() doesn't react on select with 1 option

  28. 28

    select2 - How to change the value to the first option of the list with jQuery?

  29. 29

    Detect change of option values of an select field using jquery

HotTag

Archive