javascript - 如何限制最大计数

标签 javascript jquery html

我为预订航类编写了以下代码,效果很好。 我唯一的问题是我无法对我的柜台设置限制。 我如何限制最多 4 个这样的“Child X”选择字段? o 想要设置限制以仅添加到 4 。 我怎样才能做到这一点 ? 这是我的片段:

$(function() {
  var createChildDropdown = function(i) {
    var $childDropdown = $('<div />', {
      'class': 'childs'
    });
    $childDropdown.append($('<label />', {
      'for': 'childDropdown-' + i
    }).text('Child ' + i));
    $childDropdown.append($('<select />', {
      'id': 'childDropdown-' + i
    }));
    var options = ['a', 'b', 'c'];
    options.forEach(function(option, index) {
      $childDropdown.find('select').append($('<option />')
        .text(option).attr('value', index));
    });
    return $childDropdown;
  };
  var destroyChildDropdown = function($el, i) {
    $el.find('div.childs').get(i).remove();
  };

  $(".button-click a").on("click", function() {
    var button = $(this);
    var oldVal = parseInt(button.closest("ul").prev().val());
    var newVal = (button.text() == "+") ? oldVal + 1 : (oldVal > 0) ? oldVal - 1 : 0;
    var total_value = "";

    button.closest("ul").prev().val(newVal);

    $(".travel").each(function() {
      var cat = $(this).prev('span').text();
      total_value += cat + ": " + $(this).val() + ", ";
    });

    if (oldVal < newVal) {
      $('.childDropdowns').append(createChildDropdown(newVal));
    } else if (oldVal > newVal) {
      destroyChildDropdown($('.childDropdowns'), newVal);
    }

    total_value = total_value.substring(0, total_value.length - 2);
    $(".main").val(total_value);
  })
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<label>
Count all1 Traveller(s)
<input type="text" name="no_travellers" class="main" value="Adults: 1" placeholder="" />
</label>
<br/>
<label>
   <span>Children</span>
   <input type="text" class="travel" id="CAT_Custom_410672" name="CAT_Custom_410672"  value="0" />
   <ul class="button-group button-click">
      <li><a href="#" class="small button secondary"><i class="fa fa-plus"><span class="hide">+</span></i></a></li>
      <li><a href="#" class="small button secondary"><i class="fa fa-minus"><span class="hide">-</span></i></a></li>
   </ul>
</label>
<div class="childDropdowns"></div>

最佳答案

您只需在 $(".button-click a") 点击处理程序中添加一个 if(newVal >= 5) return; 即可。当子计数达到 5 或更多时,这将阻止进一步处理。

$(function() {
  var createChildDropdown = function(i) {
    var $childDropdown = $('<div />', {
      'class': 'childs'
    });
    $childDropdown.append($('<label />', {
      'for': 'childDropdown-' + i
    }).text('Child ' + i));
    $childDropdown.append($('<select />', {
      'id': 'childDropdown-' + i
    }));
        var options = ['a', 'b', 'c'];
    options.forEach(function(option, index) {
      $childDropdown.find('select').append($('<option />')
             .text(option).attr('value', index));
    });
        return $childDropdown;
  };
    var destroyChildDropdown = function($el, i) {
    $el.find('div.childs').get(i).remove();
  };

  $(".button-click a").on("click", function() {
    var button = $(this);
    var oldVal = parseInt(button.closest("ul").prev().val());
    var newVal = (button.text() == "+") ? oldVal + 1 : (oldVal > 0) ? oldVal - 1 : 0;
    var total_value = "";
    
    if(newVal >= 5) return; 

    button.closest("ul").prev().val(newVal);

    $(".travel").each(function() {
      var cat = $(this).prev('span').text();
      total_value += cat + ": " + $(this).val() + ", ";
    });

    if (oldVal < newVal) {
      $('.childDropdowns').append(createChildDropdown(newVal));
    } else if (oldVal > newVal) {
      destroyChildDropdown($('.childDropdowns'), newVal);
    }

    total_value = total_value.substring(0, total_value.length - 2);
    $(".main").val(total_value);
  })
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<label>
Count all1 Traveller(s)
<input type="text" name="no_travellers" class="main" value="Adults: 1" placeholder="" />
</label>
<br/>
<label>
   <span>Children</span>
   <input type="text" class="travel" id="CAT_Custom_410672" name="CAT_Custom_410672"  value="0" />
   <ul class="button-group button-click">
      <li><a href="#" class="small button secondary"><i class="fa fa-plus"><span class="hide">+</span></i></a></li>
      <li><a href="#" class="small button secondary"><i class="fa fa-minus"><span class="hide">-</span></i></a></li>
   </ul>
</label>
<div class="childDropdowns"></div>

关于javascript - 如何限制最大计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45726877/

相关文章:

javascript - 使用复选框显示 div 并隐藏另一个 div

javascript - 位于下拉列表中的元素但无法单击它

javascript - Phonegap Android 捏合缩放

javascript - jQuery 隐藏功能的问题

javascript - $ ("Iframe").contents() 适用于本地主机但不能在线使用?

html - 如何根据 body-tag 中的类设置嵌套 <div> 的样式

javascript - 分区是空的。使用 jQuery...错误?

javascript - AngularJS 过滤器刷新占位符

jquery - 孙子选择器,或如何将选择器锚定在已知元素上

javascript - 如何从 API 请求