How to check if data-rate of option is same as another selected option, then do not add to total?

Viewed 33

I have a city drop down list in my checkout page. Shipping charges add in total amount of order depends upon the selected city.

If I have an option with the same data-rate value, it adds value in total every time I change city in drop down list.

I want if dropdown option have same data-rate value then it does not add again in total amount.

Here is my code:

jQuery('#city').change(function() {
  var c = jQuery(this).find(':selected').data('rate');
  c = parseFloat(c);

  jQuery('#shipping').html(c);

  var d = jQuery('#total').html();
  d = parseFloat(d);
  d = d + c;
  jQuery('#total').html(d);
  var e = jQuery('#ordertotal').html();
  e = parseFloat(e);
  e = e + c;
  jQuery('#ordertotal').html(e);

  jQuery('#orderField').val(e);
  jQuery('#shippingRate').val(c);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script>
<p><label>City</label>
  <select id="city" class=" select city" name="city" required="true">
    <option value="" disabled selected>Select The City</option>
    <option data-rate="199" value="Islamabad">Islamabad</option>
    <option value="" disabled>Punjab Cities</option>
    <option data-rate="199" value="Ahmed Nager Chatha">Ahmed Nager Chatha</option>
    <option data-rate="199" value="Ahmadpur East">Ahmadpur East</option>
    <option data-rate="199" value="Ali Khan Abad">Ali Khan Abad</option>
    <option data-rate="199" value="Alipur">Alipur</option>
  </select>
<div id="shipping"></div>
</p>

0 Answers
Related