如果表单上有错误,请在提交后保留php表单上的值

时间:2012-12-20 06:10:47

标签: php jquery

此代码我用于在下拉列表中使用一些jqueries选择多个值

<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>

0 个答案:

没有答案