如何限制多重選擇的次數?就像我想讓用戶從多選中最多選擇3個元素一樣。如果選擇超過3,那麼我會給他一個錯誤...多重選擇HTML PHP的限制次數
0
A
回答
1
PHP中的多重選擇元素返回一個數組,所以它只是比較返回數組的count()與您想要的最大值如果計數超出限制允許併產生某種錯誤(拋出異常,觸發錯誤等)。
1
如果你想在服務器端做到這一點,請看count()
函數。例如,
HTML:
<form ...>
<select name="options[]" multiple="multiple">
<!-- options here ... -->
</select>
...
</form>
PHP:
<?php
if (isset($_POST['options']) && count($_POST['options']) <= 3) {
// OK
} else {
// Not OK
}
?>
3
下面是一個完整的例子,顯示了一個簡單的HTML表單和PHP只允許最多三個選項是選擇 - 以及額外的客戶端JavaScript獎勵,以解釋如何在提交表單之前向用戶通知錯誤。
您可以使用JavaScript來停止檢查三個以上選項的用戶,但請注意可以輕鬆避免客戶端驗證,因此您仍然需要使用PHP在服務器上進行驗證。
HTML
<!doctype html>
<head>
<meta charset="utf-8">
<title>Multiselect example</title>
</head>
<p>Please make a selection below (maximum three options!)</p>
<form method="post">
<select name="selection[]" multiple="multiple">
<!-- Put <option> elements here -->
</select>
<input type="submit">
</form>
<script src="Script.js"></script>
的JavaScript(在的script.js):
var formChange = function(e) {
var i,
num = 0,
// Obtain a list of all selected options:
nodeList = this["selection[]"];
// Loop over all options, count how many are selected.
for(i = 0; i < nodeList.length; i++) {
if(nodeList[i].selected) {
num ++;
}
}
if(num > 3) {
alert("Please do not select more than three options");
e.preventDefault();
}
};
// Attach the same function to the change and submit event.
// This will alert the user of more than three selections
// as the fourth is selected, or as the form is submitted.
// ... it will also not allow the form to submit with
// more than three checked boxes.
document.forms[0].addEventListener("change", formChange);
document.forms[0].addEventListener("submit", formChange);
PHP:
if(isset($_POST["selection"])) {
if(count($_POST["selection"]) > 3) {
die("Error: More than three check boxes were checked.");
}
// $_POST["selection"] is an array of the checked values.
}
0
看看這個js功能,假設你的多選對象「muSelect」
function checkSelected() {
var i;
var count = 0;
for (i=0; i<muSelect.options.length; i++) {
if (muSelect.options[i].selected) {
count++;
}
if (count > 3) {
alert("cant select more than three options";
// any other additionnal processing
}
}
}
,然後你可以添加此功能onClick事件
0
我知道這是一個老問題,但如果有人正在嘗試做同樣的事情,尋找答案是最好爲這個寫一個當前的答案。目前最好的辦法是用AJAX(只要我知道):
HTML:form.html
<form id="myForm" action="validate.php?q=1">
<select name="options[]" multiple="multiple">
<!-- options here ... -->
</select>
...
</form>
PHP:validate.php
<?php
switch($_REQUEST['q']){
case 0:
if(isset($_REQUEST['options']) && count($_REQUEST['options']) <=3){
echo true;
}else{
echo false;
}
break;
case 1:
if(isset($_POST['options']) && !empty($_POST['options'])){
if(count($_POST['options']) <= 3){
//OK
}else{
//NOT OK
}
}
break;
}
?>
的JavaScript(jQuery的): script.js
var form = $("#myForm");
var options = $("#select_menu option")
var selected = [];
$(options).each(function(key,option){
if(option.selected){
selected[key] = option;
}
});
$.ajax({
url: "validate.php",
method: "GET",
data: {
options: selected,
q: 0
},
success: function(Response){
if(Response === true){
//OK
}else{
//NOT OK
}
}
});
請糾正我,如果我有錯誤的地方。
相關問題
- 1. HTML多選擇限制
- 2. Html選擇,選擇限制
- 3. Ionic2,離子選擇多重限制用戶選擇
- 4. 限制多個選擇
- 5. 多重選擇字段 - 選擇=「選擇」多次
- 6. HTML選擇+可選選項的數量限制
- 7. HTML和PHP選擇多個
- 8. 限制選擇二至每OPTGROUP一個選擇,在版本多重選擇4.0
- 9. PHP PDO多重選擇,多重條件
- 10. 選擇與雙重限制的MySQL
- 11. 限制用戶在PHP中選擇的複選框的數量
- 12. 選擇具有多個限制的值
- 13. HTML表單多重選擇使用GET
- 14. Android:限制選擇的數量多選列表視圖
- 15. 如何在多個選擇插件中選擇限制數量的選項?
- 16. Django - 限制選擇字段的選擇
- 17. 選擇與限制
- 18. 限制次數
- 19. 雙抽頭選擇燒製多次
- 20. 製作SQL多次選擇同一行
- 21. HTML多重選擇到Ajax URL
- 22. 限制選擇選項
- 23. 限制選擇複選框
- 24. mongoDB「多重」選擇
- 25. PHP開關罩HTML多種選擇
- 26. 從數據庫的PHP多個下拉列表 - 如何限制選擇3
- 27. 限制easyui combobox中的選擇數量
- 28. 後兩個選擇與多重選擇
- 29. 限制ForeignKey的選擇
- 30. MySQL的子選擇限制
我不能在客戶端做到這一點?喜歡使用一些javascript – Jewel
是的,但你用PHP標記了你的問題,這是一種服務器端語言。如果你想要一個客戶端的答案,然後用javascript代替你的問題。 – GordonM
即使使用客戶端驗證,您也應該始終進行服務器端驗證,因爲沒有任何措施可以阻止用戶關閉客戶端大小驗證。 – GordonM