0

我使用此代码在下拉列表中选择多个值,其中包含一些 jquery

<script type="text/javascript">           
    $(document).ready(function() {
        $("#s1").dropdownchecklist();
        $("#s2").dropdownchecklist( {icon: {}, width: 150 } );
        $("#s3").dropdownchecklist( { width: 150 } );
        $("#s4").dropdownchecklist( { maxDropHeight: 150 } );
        $("#s5").dropdownchecklist( 
            { firstItemChecksAll: true, explicitClose: '...close' } );
        $("#s6").dropdownchecklist();
        $("#s7").dropdownchecklist();
        $("#s8").dropdownchecklist( { emptyText: "Please Select...", width: 150 } );
        $("#s9").dropdownchecklist( { textFormatFunction: function(options) {
            var selectedOptions = options.filter(":selected");
            var countOfSelected = selectedOptions.size();
            switch(countOfSelected) {
                case 0: return "<i>Nobody<i>";
                case 1: return selectedOptions.text();
                case options.size(): return "<b>Everybody</b>";
                default: return countOfSelected + " People";
            }
        } 
    });
    $("#pcity").dropdownchecklist({ emptyText: "Please select ...", width: 500 },{ maxDropHeight: 150 }, { forceMultiple: true
, onComplete: function(selector) {
    var values1 = "";
    for( i=0; i < selector.options.length; i++ ) {
        if (selector.options[i].selected && (selector.options[i].value != "")) {
            if ( values1 != "" ) values1 += ",";
                values1 += selector.options[i].value;               
        }
    }
    alert( values1 );
} 
, onItemClick: function(checkbox, selector){
    var justChecked = checkbox.prop("checked");
    var checkCount = (justChecked) ? 1 : -1;
    for( i = 0; i < selector.options.length; i++ ){
        if ( selector.options[i].selected ) checkCount += 1;
    }
    if ( checkCount > 3 ) {
        alert( "Limit is 3" );
        throw "too many";
    }
}
            });

我有这个城市下拉菜单的代码,但是当提交后表单上出现一些错误时,值不会保留在表单上,​​但是如果出现一些错误,我没有使用上述脚本值的其他下拉菜单上的相同代码会在提交后保留在表单上,请有一些建议,谢谢。

<tr>
    <td> 
        <select name="pcity[]" id="pcity" multiple="multiple" style="display: none">
        <?php
            $pcitylist =  array(
        'Andaman and Nicobar' => array(
        'North and Middle Andaman', 'South Andaman', 'Nicobar'
    ),
    'Andhra Pradesh' => array(
        'Adilabad', 'Anantapur', 'Chittoor', 'East Godavari', 'Guntur', 'Hyderabad', 'Kadapa', 'Karimnagar', 'Khammam', 'Krishna', 'Kurnool', 'Mahbubnagar', 'Medak', 'Nalgonda', 'Nellore', 'Nizamabad', 'Prakasam', 'Rangareddi', 'Srikakulam', 'Vishakhapatnam', 'Vizianagaram', 'Warangal', 'West Godavari'
    ),
    'Arunachal Pradesh' => array(
        'Anjaw', 'Changlang', 'East Kameng', 'Lohit', 'Lower Subansiri', 'Papum Pare', 'Tirap', 'Dibang Valley', 'Upper Subansiri', 'West Kameng'
    ),
    );

    foreach ($pcitylist as $key => $pcitylist1)
    {
      echo '<optgroup label="'.$key.'">';
      foreach ($pcitylist1 as $finalCity) {
          echo '<option value="' . $finalCity . '"' . (isset($_POST['pcity']) && $_POST['pcity'] == $finalCity ? ' selected' : '') . '>' . $finalCity . '</option>';
      }   
      echo '</optgroup>';
} 
?>
</select>

    </td>
</tr>
4

0 回答 0