[英]How do you limit options selected in a html select box?
我在允許多項選擇的表單中使用 select 標簽,但我想最多選擇 10 個。這是否可能使用 javascript 或 jquery?
提前致謝!
這里有一些完整的代碼供您使用...得熱愛Google AJAX API游樂場:-)
編輯1:注意:這只能讓你選擇5,因為我不想復制/粘貼另外10個選項:-)
<!--
copyright (c) 2009 Google inc.
You are free to copy and use this sample.
License can be found here: http://code.google.com/apis/ajaxsearch/faq/#license
-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Sample Select Maximum with jQuery</title>
<script src="http://www.google.com/jsapi?key=ABQIAAAA1XbMiDxx_BTCY2_FkPh06RRaGTYH6UMl8mADNa0YKuWNNa8VNxQEerTAUcfkyrr6OwBovxn7TDAH5Q"> </script>
<script type="text/javascript">
google.load("jquery", "1");
$(document).ready(function() {
var last_valid_selection = null;
$('#testbox').change(function(event) {
if ($(this).val().length > 5) {
alert('You can only choose 5!');
$(this).val(last_valid_selection);
} else {
last_valid_selection = $(this).val();
}
});
});
</script>
</head>
<body style="font-family: Arial;border: 0 none;">
<select multiple id='testbox'>
<option value='1'>First Option</option>
<option value='2'>Second Option</option>
<option value='3'>Third Option</option>
<option value='4'>Fourth Option</option>
<option value='5'>Fifth Option</option>
<option value='6'>Sixth Option</option>
<option value='7'>Seventh Option</option>
<option value='8'>Eighth Option</option>
<option value='9'>Ninth Option</option>
<option value='10'>Tenth Option</option>
</select>
</body>
</html>
這會將用戶限制為3個選項:
$("select").on("click", "option", function () {
if ( 3 <= $(this).siblings(":selected").length ) {
$(this).removeAttr("selected");
}
});
小提琴: http : //jsfiddle.net/2GrYk/
這個答案適用於這些情況:
碼:
$('#mySelect').on('change', function() {
if (this.selectedOptions.length < 4) {
$(this).find(':selected').addClass('selected');
$(this).find(':not(:selected)').removeClass('selected');
}else
$(this)
.find(':selected:not(.selected)')
.prop('selected', false);
});
或者,對於不支持selectedOptions
舊瀏覽器,
$('#mySelect').on('change', function() {
var $sel = $(this).find(':selected');
if ($sel.length < 4) {
$sel.addClass('selected');
$(this).find(':not(:selected)').removeClass('selected');
}else
$(this)
.find(':selected:not(.selected)')
.prop('selected', false);
});
使用jQuery,您可以將函數附加到change事件,因為它是一個多重選擇,val()將是一個數組。 您可以隨時檢查數組的長度,並在其預定義大小的情況下執行某些操作。 以下代碼演示了如何知道選擇了多少項。
$("#select").change(function(){
alert($(this).val().length);
});
弄清楚了! 這是結果代碼:
$(document).ready(function(){
var last_valid_selection = null;
var selected = "";
$("#recipient_userid").change(function(event){
if ($(this).val().length > 10) {
alert('You can only choose 10!');
$(this).val(last_valid_selection);
} else {
last_valid_selection = $("#recipient_userid").val();
$("#recipient_userid option:selected").each(function () {
selected += "<li>" + $(this).text() + "</li>";
});
$("#currentlySelected").html(selected);
selected = "";
}
}).change();
});
謝謝你們的幫助!
我基本上合並了一些提供的答案來制作特定ID的特定內容:
$("#mySelect").on("click", "option", function(){
var max = 3;
if ( max <= $(this).siblings(":selected").length ) {
alert("Only " + max + " selections allowed.");
$(this).removeAttr("selected");
}
});
搶
你可以使用jQuery
$("select").change(function () {
if($("select option:selected").length > 3) {
//your code here
}
});
瞧! 不需要Jquery。
var is = 0;
for (var i = 0; i < selectCountryCode.length; i++) {
if (selectCountryCode.options[i].selected) {
is++;
}
if (is > 3) {
selectCountryCode.options[i].selected = false;
}
}
Jilusan
這是一個純香草 JavaScript 示例,基於 jilu 的示例。
const selectEls = document.querySelectorAll('select[multiple]');
selectEls.forEach((selectEl) => {
selectEl.addEventListener('change', (e) => {
const currentEl = e.currentTarget;
let is = 0;
for (let i = 0; i < currentEl.length; i++) {
if (currentEl.options[i].selected) {
is++;
}
if (is > 3) { // if you want more than 3 selected options, change the number here
currentEl.options[i].selected = false;
}
}
})
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.