如何使选择标记无法选择或隐藏

时间:2017-03-20 07:24:43

标签: jquery

当我输入的值为0时,如何使我的成人和儿童选择标签变得无法选择或隐藏。另外,当我点击'时,如何停止输入值变为小于零? '按钮。



$(function(){
  var p = 2500;
  $('#add').click(function(){
    value = $("#rooms").val();
    price = $("#price").val();
    priceInt = parseInt(price);
    value++;
    price = priceInt + p; 
    $("#rooms").val(value);
    $('#price').val(price);
  });
  $('#minus').click(function(){
    value = $("#rooms").val();
    price = $("#price").val();
    priceInt = parseInt(price);
    value--;
    price = priceInt - p;
    $("#rooms").val(value);
    $('#price').val(price);
  });
  var adult = $( "#selectAdult option:selected" ).text();
  adultInt = parseInt(adult);
  price = $("#price").val();
});

<html>
  <body>
    <input type = "button" id="minus" value="-">
    <input type="text" id="rooms" value="1">
    <input type = "button" id="add" value="+">
    Adult: <select id="selectAdult">
      <option>1</option>
      <option selected="selected">2</option>
      <option>3</option>
    </select>
    Child: <select>
      <option>0</option>
      <option selected="seelcted">1</option>
      <option>2</option>
    </select>
    Total: <input type="text" id="price" value="2500">
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  </body>
</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

希望这就是你要找的东西。

  • - 上单击,首先询问该值是否大于0.然后允许它向下1值。然后询问新值是否为0,如果是,则禁用选择框。
  • + 上单击,询问该值是否大于0,如果是,则启用选择框

&#13;
&#13;
$(function() {
  var p = 2500;
  $('#add').click(function() {
    value = $("#rooms").val();
    price = $("#price").val();
    priceInt = parseInt(price);
    value++;
    price = priceInt + p;
    $("#rooms").val(value);
    $('#price').val(price);
    if (value > 0) {
      $('#selectAdult, #selectChild').prop('disabled', false);
    }
  });
  $('#minus').click(function() {
    value = $("#rooms").val();
    if (value > 0) {
      price = $("#price").val();
      priceInt = parseInt(price);
      value--;
      price = priceInt - p;
      $("#rooms").val(value);
      $('#price').val(price);
    }
    if (value == 0) {
      $('#selectAdult, #selectChild').prop('disabled', 'disabled');
    }
  });
  var adult = $("#selectAdult option:selected").text();
  adultInt = parseInt(adult);
  price = $("#price").val();
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>

<body>
  <input type="button" id="minus" value="-">
  <input type="text" id="rooms" value="1" readonly="readonly">
  <input type="button" id="add" value="+"> Adult: <select id="selectAdult">
      <option>1</option>
      <option selected="selected">2</option>
      <option>3</option>
    </select> Child: <select id="selectChild">
      <option>0</option>
      <option selected="seelcted"> 1</option>
      <option>2</option>
    </select> Total: <input type="text" id="price" value="2500">
</body>

</html>
&#13;
&#13;
&#13;