如果找不到数据,请将选择值更改为null

时间:2016-02-08 12:12:10

标签: javascript php html

如果在选择字段中找不到数据量,我想将所选值更改为null。例如,我在数量字段中输入10没有数据量=" 10"在我的选择选项标签中,所以它应该为null 总价格应该等于0。
enter image description here

项目价格:

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

数量:

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

送货详情:

<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 be 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>

总价:

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

1 个答案:

答案 0 :(得分:0)

我认为你正在寻找像

这样的东西
<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 be 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>

检查工作jsfiddle