2017-08-17 97 views
0

我爲預訂航班編寫了下面的代碼,它效果很好。 我唯一的問題是我無法對我的櫃檯設置限制。 如何限制最多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>

+0

你應該順便 –

回答

2

你可以只在您的$(".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>

+0

優化你的代碼太謝謝你了! – inaz