来自数据库的PHP多个下拉列表 - 如何将选择限制为3

时间:2017-06-02 19:31:39

标签: php html drop-down-menu multipleselection

我使用HTML / PHP创建了一个下拉列表,用于选择学生的多个资格。它工作正常,但我想将选择仅限于3个资格。任何高级成员都可以指导我如何使用PHP / MySql / HTML实现它。

这是代码

<select required id="JOBQUALIFICATION" name="JOBQUALIFICATION[]" multiple="multiple" size="5" style="width:300px">
    <option value="">-Select-</option>
    <?php
        do {  
    ?>
    <option value="<?php echo $row_qmast['QMDESC']?>"><?php echo $row_qmast['QMDESC']?></option>
    <?php
        } while ($row_qmast = mysql_fetch_assoc($qmast));
    ?>
</select>

<?php

if(isset($_POST['JOBQUALIFICATION'])) {

$JOBQUALIFICATION=implode(',',$_POST['JOBQUALIFICATION']);
} else  {

$JOBQUALIFICATION="";
}
echo $JOBQUALIFICATION;
?>

1 个答案:

答案 0 :(得分:0)

在选择3之后禁用选择

<html>
<tite></title>
<head>
<!-- Import the Jquery files -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head
<!-- Jquery Script --> 
<script>
    $(document).ready(function(){

        $("#JOBQUALIFICATION").change(function () {
              if($("#JOBQUALIFICATION option:selected").length > 3) {
                   $('#JOBQUALIFICATION').prop('disabled', false);
              }
          });

    });
        </script>
    <body>




<select required id="JOBQUALIFICATION" name="JOBQUALIFICATION[]" multiple="multiple" size="5" style="width:300px">
    <option value="">-Select-</option>
    <?php
        do {  
    ?>
    <option value="<?php echo $row_qmast['QMDESC']?>"><?php echo $row_qmast['QMDESC']?></option>
    <?php
        } while ($row_qmast = mysql_fetch_assoc($qmast));
    ?>
</select>

<?php

if(isset($_POST['JOBQUALIFICATION'])) {

$JOBQUALIFICATION=implode(',',$_POST['JOBQUALIFICATION']);
} else  {

$JOBQUALIFICATION="";
}
echo $JOBQUALIFICATION;
?>

</body>
</html>