繁体   English   中英

设置HTML <select>选项是基于当前时间的默认值

[英]setting html <select> option to be the default value based on current time

我已经制作了一个基本的打孔时钟网页和MySQL数据库,唯一的问题是,当人们无意间而不是一天结束时才进来,反之亦然,这会留下很多空白。

我需要某种代码,这些代码的默认值是“ startfinish”值在10:30之前为“开始”,在14:30之后为“结束”,但是如果需要,用户仍然可以更改选项

请注意,真的确定从哪里开始,以及我是否应该使用javascript或php(php在单独的文件中作为“表单操作”)

这是我当前需要更改选项的html代码:

<select name="startfinish" id="startend" required>
                <option selected="selected" disabled selection>Please select</option>
                <option value="Start">Start</option>
                <option value="End">End</option>

任何帮助将不胜感激

谢谢,

丹尼尔

您可以使用Date对象和一些jQuery获得所需的结果: http : //jsfiddle.net/8cuL0k3h/

$(function() {

    // Grab the date object and get hours/minutes
    var current = new Date();
    var hours = current.getHours();
    var minutes = current.getMinutes();

    // Check if time is before 10:30am
    if( hours <= 10 && minutes < 30 ) {
        $('#startend').val('Start');
    }

    // Check if time is after 2:30pm
    else if( hours >= 14 && minutes > 30 ) {
        $('#startend').val('End');
    }
});

如果您不反对PHP,则可以检查当前时间,并将其与指定的时间进行比较。 但是,这必须放在您正在使用的文件中,而不是动作文件中。

<?php

// Set the default timezone so you don't run into timezone issues
// You can set this to whatever suits your application best
// Full list of supported timezones here: http://php.net/manual/en/timezones.php
date_default_timezone_set('America/Vancouver');

// Compare the current time to the start and end times
// This reads: if current time is before start time, option is selected, otherwise it's not
$start = (strtotime('now') < strtotime('10:30 AM today') ? 'selected="selected"' : '');
// This reads: if current time is after end time, option is selected, otherwise it's not
$end   = (strtotime('now') > strtotime(' 2:30 PM today') ? 'selected="selected"' : '');

?>

要在选择控件中使用此功能,您需要在选项中回显变量(为简便起见,以简写形式显示)。 如果日期计算正确,则将选择该选项,否则它将保持不变。 我从占位符中删除了该选择,因为此设置将确保始终选择“开始”或“结束”。

<select name="startfinish" id="startend" required>
  <option disabled selection>Please select</option>
  <option <?=$start?> value="Start">Start</option>
  <option <?=$end?> value="End">End</option>
</select>

在这种情况下使用PHP的好处是它运行在服务器端而不是客户端,因此您不必担心用户禁用Javascript并破坏您的表单设计。 但是,如果您已经在应用程序中依赖jQuery,则可能不是问题。

我假设该网站是静态生成的,在这种情况下,PHP不会对这个特定的苹果产生影响。 这是仅JS方法。

<!DOCTYPE html>
<html>
<head>
<script>
"use strict";
function byId(e){return document.getElementById(e);}

window.addEventListener('load', onDocLoaded, false);

function onDocLoaded()
{
    initSelectElement();
}

function makeItemSelected(listElem, index)
{
    var i, n = listElem.options.length;

    for (i=0; i<n; i++)
    {
        if (index == i)
            listElem.options[i].setAttribute('selected','');
        else
            listElem.options[i].removeAttribute('selected');
    }
}

function initSelectElement()
{
    var curTime = new Date();
    var curHour = curTime.getHours();
    var curMin = curTime.getMinutes();

    if ((curMin <= 30) && (curHour <= 10))
    {
        makeItemSelected(byId('startend'), 1);
    }

    else if ((curMin >= 30) && (curHour >= 2))
    {
        makeItemSelected(byId('startend'), 2);
    }
}
</script>
<style>
</style>
</head>
<body>
    <select name="startfinish" id="startend" required>
                <option selected disabled>Please select</option>
                <option value="Start">Start</option>
                <option value="End">End</option>
    </select>
</body>
</html>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM