javascript - Change select value to null if there is no data found on it -


guys want change selected value null if there no data-quantity not found in select field. example enter 10 on quantity field there no data-quantity = "10" in select option tag should null , total price should equal 0 .
enter image description here

price of item:

 <input type="hidden" name="price" id="price"class="price" value="<?php if($price!=""){echo $price;}else{echo $shop_item_orig_price;}?>"> 

quantity:

<input type="number" name="quant" id="quant" /> // example enter 2 

shipping details:

<select name="shipment" disable>  <option value="100"  data-quantity="1"> 100 per 1 item </option>  <option value="150"  data-quantity="2"> 150 per 2 item </option> //this must selected </select>  <script> function myfunction(quantinput) {   var price = document.getelementbyid("price");   var quantity = document.getelementbyid("quant");   var shipment = document.getelementbyid("shipmentselect");   var total = document.getelementbyid("total");     $("#shipmentselect").find("option[data-quantity=" + quantinput + "]").attr('selected', 'selected').siblings().removeattr('selected');   var shipmentvalue = shipment.value;   saleprice = price.value;   var totalprice = (saleprice*quantinput) + parseint(shipmentvalue);   total.value = totalprice; } </script> 

total price:

<input id="total" name="answer" style="margin-top:10px;margin-left:5px;"readonly /> 

i think looking like

<input type="number" name="quant" value="" id="quant" /> <select name="shipment" class="select_custom" disable>     <option value="100"  data-quantity="1"> 100 per 1 item </option>     <option value="150"  data-quantity="2"> 150 per 2 item </option> //this must selected </select>  <input type="hidden" name="price" id="price"class="price" value="<?php if($price!=""){echo $price;}else{echo $shop_item_orig_price;}?>">  <script>     $(document).ready(function () {         $("#quant").focusout(function () {             var qty = $(this).val();             var price = $("#price").val();             $(".select_custom option").each(function () {                 console.log($(this).attr('data-quantity'));                 if ($(this).attr('data-quantity') == qty) {                     $("#quant").val(qty);                     $("#price").val(price);                     return false;                 } else {                     $("#quant").val(0);                     $("#price").val(0);                     $(".select_custom").append('<option value=null selected>null</option>').selectmenu('refresh');                 }             });         });     }); </script> 

check working jsfiddle


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -