如何添加选择列表的限制

时间:2017-03-16 20:41:49

标签: jquery html5 css3

您好我是jQuery的新手,我想让人们只选择5个类别和每个选择&删除,数量应减少&增加而不是这个"你最多可以添加5个类别"它应该显示这个"你可以再添加4个类别。"



$(document).ready(function() {
	var one = $('.select-manage-category').val();
	var two = $('.select-manage-category1').val();
	var three = $('.select-manage-category2').val();
	$('.select-manage-category, .select-manage-category1, .select-manage-category2').change(function() {
		var one = $('.select-manage-category').val();
		var two = $('.select-manage-category1').val();
		var three = $('.select-manage-category2').val();
		if (one && two && three) {
			$("#add-category").prop("disabled", false).css({
				'font-weight': 'bold'
			});
		} else {
			$("#add-category").prop("disabled", true).css({
				'font-weight': 'normal'
			});
		}
	});
	$('#add-category').click(function() {
		$('#selected-lst-values').append('<option value="' + $(this).val() + '">' + $('.select-manage-category').val() + ' << ' + $('.select-manage-category1').val() + ' << ' + $('.select-manage-category2').val() + '</option>');
	});
  
	$('#selected-lst-values option').each(function() {
		if ($(this).val() == '') {
    alert('ffdgdfgf');
			$('#remove-category').prop('disabled', true).css({
				'font-weight': 'normal'
			});
		} else {
			$('#remove-category').prop('disabled', false).css({
				'font-weight': 'bold'
			});
		}
	});
  
  	$('#remove-category').click(function() {
		$('#selected-lst-values option:selected').remove();
	});
});
&#13;
.select-manage-category,
.select-manage-category1,
.select-manage-category2 {
  width: 100px;
  float: left;
  margin-right: 4px;
}

p {
  clear: left;
  text-align: center;
}

#selected-lst-values {
  width: 100%;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div><select class="form-control select-manage-category" size="5">
    <option>Paper Manufacturers</option>
    <option>Paper Products Suppliers</option>
    <option>Paper Chemicals Suppliers</option>
</select></div>

<div><select class="form-control select-manage-category1" size="5">
    <option>Paper Converters</option>
    <option>Lab Apparatus & Supplies</option>
    <option>Service Providers</option>
		</select></div>
<div><select class="form-control select-manage-category2" size="5">
    <option>Molded Pulp Products</option>
    <option>Software Vendors</option>
    <option>Information Services</option>
		</select>
</div>
<p style="padding-top:10px;color:red;">You can add up to 5 categories</p>
</div>
<input id="add-category" name="add" type="button" value="Add Category" disabled="true">
<input id="remove-category" name="add" type="button" value="Remove Category" disabled="true">
<div><select id="selected-lst-values" class="form-group percent-100" size="7" multiple="multiple">
							</select></div>
<button id="mnage-category-savebtn" class="btn btn-md btn-radius pi-btn prodetails-btn" type="button"><strong>Save Categories</strong> <span class="glyphicon glyphicon-menu-right right-arrow-head-icon"></span></button>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

这是一个可以处理显示有多少可用选择的工作版本。由于睡眠过程下降,代码有点混乱......

<pre> <code> https://jsfiddle.net/at8ahu7c/2/ </code> </pre> 

但只是问你是否有问题,希望它有所帮助!欢呼!!