在选择下拉菜单上设置实时刷新 [英] Setting up a live refresh on select drop down

查看:86
本文介绍了在选择下拉菜单上设置实时刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前已经设置了一个选择下拉菜单,该菜单执行以下三项操作:

I have currently set up a select drop down menu that does three things:

  • 默认情况下,它会选择未来48小时和当前月份的日期
  • 要防止选择过去的日期,它只会显示所选日期,直到当前月末为止.
  • 系统的另一个局限性在于它显示了日历年末之前的所有月份

但是现在我有一个问题!让我用这个例子来说明我的困难

But now I have a problem! Let me use this example to explain my difficulty

  • 在撰写本文时,默认情况下日期为9月23日. 9月23日之前没有其他日期
  • 用户可以选择从9月23日至9月31日的日期
  • 用户还可以选择从9月到12月的月份

现在,当用户尝试从另一个月份中选择一个日期时,就会出现问题.日期限制仍然存在!因此,如果我切换到10月,则只能选择10月23日至10月31日之间的日期.我想选择10月1日至10月31日之间的日期是错误的.

The problem now arises when the user tries to select a date from a different month. The date limitation still exists! So if I switch to October I can only select dates from 23 October to 31 October. Which is wrong cos I want to select dates from 1 October to 31 October.

当用户切换到另一个月时,如何实时刷新下拉列表以更新日期条目?

How can I refresh the drop down in real-time to update the date entry when the user switches to another month?

这是我现在正在使用的代码:

Here is the code I'm using right now:

<label for="date"><?php echo __('Pickup Date') ?> <span>*</span></label>
<?php
$curr_day = date('j', strtotime('+ 48 hours'));
$day = range (1, 31);
$day = array_slice($day, $curr_day-1);
$select = "<select name=\"day\">\n";
foreach ($day as $key => $val) {
    $select .= "\t<option val=\"".$key."\"";
    if ($key == $curr_day) {
        $select .= " selected=\"selected\">".$val."</option>\n";
    } else {
        $select .= ">".$val."</option>\n";
    }
}
$select .= "</select>";
echo $select;
?>
    &nbsp;:&nbsp;
<?php 
$curr_month = date("m");
$month = array (1=>"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
$month = array_slice($month, $curr_month-1);
$select = "<select name=\"month\">\n";
foreach ($month as $key => $val) {
    $select .= "\t<option val=\"".$key."\"";
    if ($key == $curr_month) {
        $select .= " selected=\"selected\">".$val."</option>\n";
    } else {
        $select .= ">".$val."</option>\n";
    }
}
$select .= "</select>";
echo $select;
?>

非常感谢!

推荐答案

您将使用JavaScript有效地做到这一点,如果您敏捷的话,可以使用jQuery.

You're going to use JavaScript to do it effectively, jQuery if you're nimble.

$('select').change(function(){
    updatetheselect;
});

为此,请制作一个PHP页面,该页面使用json_encode()在json数组中输出值:

To do it, make a PHP page that outputs the values in a json array using json_encode():

{"option1":"value"}

然后使用AJAX检索信息并解析它:

Then use AJAX to retrieve the info and parse it:

$.getJSON('jsonstuff.php',{month:'august'},function(json) {
    json.each(function(key,value){
        $('select').append('<option value="'+key+'">'+value+'</option>');
    });
});

此功能未经测试.

http://jsfiddle.net/tBrXt/2/

这篇关于在选择下拉菜单上设置实时刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆