duanmu2015 2015-11-07 22:04
浏览 28
已采纳

使用下拉列表PHP

i have two questions. this is my code (Iranian calender), the problem is when i want see results. all values of year and month and day showns as '$year' and 'month' and 'day'.

enter image description here

 <?php

            echo'<select dir="rtl" name="year">';
            for($year=1388;$year<=1410;$year++)
            { 
            echo '<option value="$year">$year</option>';


            } 
echo'</select>';


echo'<select dir="rtl" name="month">';
for($month=1;$month<=12;$month++)
            {


            echo '<option value="$month">$month</option>';

            }
echo'</select>';


echo'<select dir="rtl" name="day">';
for($day=1;$day<=31;$day++)
            {


            echo '<option value="$day">$day</option>';

            }
echo'</select>';
            ?>

second question:

and i need another thing. in Iranian calendar month 1 to 6 have 31 days and 7 to 12 have 30 days so i need a conditional expression when user choose month 1 to 6 show 31 days to choose and when user chooses month 7 to 121 show 30 days.

  • 写回答

2条回答 默认 最新

  • doufeixuan8882 2015-11-07 22:07
    关注

    You use double quotes inside of single quotes so it will be rendered as string. Make the changes accordingly:

    From:

    echo '<option value="$year">$year</option>';
    

    To:

    echo '<option value="'.$year.'">'.$year.'</option>';
    
    echo '<option value="'.$month.'">'.$month.'</option>';
    
    echo '<option value="'.$day.'">'.$day.'</option>';
    

    EDIT (answer to your second question):

    JSFIDDLE

    First you need to give an id attribute to your select fields.

    <select dir="rtl" name="month" id="month">
    
    <select dir="rtl" name="day" id="day">
    

    Then you need to add some JQuery code that checks if month is greater than 6 so it will remove the last option (only if days are 31). If month is equal of 6 or less then will add a new option (only if days are 30).

    $('#month').on('change', function() {
        var monthValue = $(this).val();
        var dayOptions = $('#day option').size();
        if (monthValue > 6 && monthValue <=12) {
            if (dayOptions == 31) {
                $("#day option:last").remove();
            }
        } else if (monthValue >= 1 && monthValue <= 6) {
            if (dayOptions == 30) {
                $('#day').append($('<option>', {
                        value: 31,
                        text: 31
                    }));
            }
        }
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助