简体   繁体   English

如何限制最大计数

[英]How to limit the maximum of count

I wrote the below code for a booking flight and it works good. 我为预订航班编写了以下代码,效果很好。 my only problem is i can not set a limitation to my counter. 我唯一的问题是我不能对我的柜台设置限制。 how can i limit a maximum of 4 such “Child X” select fields? 如何最多限制4个此类“ Child X”选择字段? o want to set a limitation to add just to 4 . o希望设置一个限制,使其仅增加到4。 how can i do this ? 我怎样才能做到这一点 ? here is my snippet : 这是我的片段:

 $(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> 

You could just add a if(newVal >= 5) return; 您可以只添加一个if(newVal >= 5) return; in your $(".button-click a") click handler. $(".button-click a")单击处理程序中。 That would prevent further processing whenever the child count is going to be 5 or more. 每当子计数为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> 

如何删除默认的最大字数限制(maxlength)<textarea> &lt;i&gt;&lt;/div&gt;&lt;/i&gt; &lt;b&gt;&lt;/div&gt;&lt;/b&gt;</textarea><div id="text_translate"><p> 我正在使用&lt;textarea&gt;字段从客户端获取输入。</p><p> 默认情况下, &lt;textarea&gt;的最大长度为maxlength来源:<a href="https://www.geeksforgeeks.org/html-textareamaxlength-attribute/" rel="nofollow noreferrer">Textarea</a>的最大长度</p><p>我希望&lt;textarea&gt;是<strong>无限</strong>的,因为客户希望使用它来处理大量文本。</p><p> 如何做到这一点?</p><p> 我尝试了什么:</p><p> 我用过maxLength={Number.MAX_SAFE_INTEGER} 。 这会将最大长度设置为maxlength (9007199254740991) 中的最大安全 integer。</p><p> 我正在寻找其他更好的方法。</p></div> - How to remove default maximum word limit (maxlength) from <textarea>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在javascript中设置最大限制? - How to put maximum limit in a javascript? 如何限制上传的图像文件的最大数量 - how to limit the maximum number of uploaded image files Node JS如何限制最大套接字数 - Node JS how to limit maximum number of sockets 如何计算javascript中最大登录尝试次数? - how to count maximum number of login attempts in javascript? 如何限制 HTML 中文本区域的字数 - How to limit word count of a textarea in HTML 如何限制用户最多打开5个Bootstrap Tab-Panes? - How to limit user from opening Bootstrap Tab-Panes with maximum 5? Extjs 4仪表图,如何动态改变最大极限值? - Extjs 4 gauge chart, how to change the maximum limit value dynamically? 如何删除默认的最大字数限制(maxlength)<textarea> &lt;i&gt;&lt;/div&gt;&lt;/i&gt; &lt;b&gt;&lt;/div&gt;&lt;/b&gt;</textarea><div id="text_translate"><p> 我正在使用&lt;textarea&gt;字段从客户端获取输入。</p><p> 默认情况下, &lt;textarea&gt;的最大长度为maxlength来源:<a href="https://www.geeksforgeeks.org/html-textareamaxlength-attribute/" rel="nofollow noreferrer">Textarea</a>的最大长度</p><p>我希望&lt;textarea&gt;是<strong>无限</strong>的,因为客户希望使用它来处理大量文本。</p><p> 如何做到这一点?</p><p> 我尝试了什么:</p><p> 我用过maxLength={Number.MAX_SAFE_INTEGER} 。 这会将最大长度设置为maxlength (9007199254740991) 中的最大安全 integer。</p><p> 我正在寻找其他更好的方法。</p></div> - How to remove default maximum word limit (maxlength) from <textarea> 如何在 Vuejs 中使用验证消息设置用户名的最大限制? - How to set maximum limit for username with validation message in Vuejs? 如何限制本地存储的最大项目? - How can I limit the local storage maximum item?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM