gpt4 book ai didi

javascript - 如何根据输入的小时添加 PM 或 AM?

转载 作者:行者123 更新时间:2023-12-03 11:16:57 24 4
gpt4 key购买 nike

我有一个带有一些功能的时钟。现在,我从该输入中的时钟获得结果:

  <input class="arrow tag-input tag-input-mobile" type='Number' id='number2' placeholder='7:00 PM' class='qty' max="10" min="0" data-toggle="modal" data-target="#exampleModalTime"/>

我只从时钟获取值,但我需要有 PM/AM,基于该值,我如何使用 JavaScript 添加它?

这是我的代码:

function increaseValue() {
var value = parseInt(document.getElementById('number').value, 10);
value = isNaN(value) ? 0 : value;
value++;
if (value > 24) {
return
}
document.getElementById('number').value = value;
document.getElementById('number2').value = value;
}

function decreaseValue() {
var value = parseInt(document.getElementById('number').value, 10);
value = isNaN(value) ? 0 : value;
value < 1 ? value = 1 : '';
value--;
document.getElementById('number').value = value;
document.getElementById('number2').value = value;
}

function maxLengthCheck(object) {
if (object.value.length > object.maxLength)
object.value = object.value.slice(0, object.maxLength)
}

function isNumeric(evt) {
var theEvent = evt || window.event;
var key = theEvent.keyCode || theEvent.which;
key = String.fromCharCode(key);
var regex = /[0-9]|\./;
if (!regex.test(key)) {
theEvent.returnValue = false;
if (theEvent.preventDefault) theEvent.preventDefault();
}
}
<div class="modal fade" id="exampleModalTime" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">




<div class="wrapper-clock" id="wrapper">
<div id="navigator">

<div id="calendar">

<div id="daysblock3">

<div class="bottom-items">

<img class="bg-clock" src="images/clock/bg-clock.png">
<img class="left-up" src="images/clock/left-up.png" id="increase" onclick="increaseValue()" value="Increase Value">
<img class="right-up" src="images/clock/right-up.png" id="increaseR" onclick="increaseValueR()" value="Increase ValueR">
<img class="right-down" src="images/clock/right-down.png" id="decreaseR" onclick="decreaseValueR()" value="Decrease Value">
<img class="left-down" src="images/clock/left-down.png" id="decrease" onclick="decreaseValue()" value="Decrease Value">
<img class="line-time" src="images/clock/line.png">

<p class="text-pm">PM</p>
<input class="text-hour" type='Number' id='number' value='7' class='qty' max="10" min="0" maxlength="2" oninput="maxLengthCheck(this)" />
<input class="text-minutes" type='Number' id='numberR' value='00' class='qty' max="10" min="0" maxlength="2" oninput="maxLengthCheckR(this)" />

</div>
</div>

</div>
</div>


</div>
</div>
</div>

所以,我只需要在那个输入中添加那种文本 document.getElementById('number2').value = value; 所以,从 0 到 12 应该有 AM,从 13到 24 应该是下午。

最佳答案

您也可以使用选择方法。这是示例:

<!DOCTYPE html>
<html>
<body>

<select id="mySelect" onchange="myFunction()">
<option value=0>0</option>
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
<option value=4>4</option>
<option value=5>5</option>
<option value=6>6</option>
<option value=7>7</option>
<option value=8>8</option>
<option value=9>9</option>
<option value=10>10</option>
<option value=11>11</option>
<option value=12>12</option>
<option value=13>13</option>
<option value=14>14</option>
<option value=15>15</option>
<option value=16>16</option>
<option value=17>17</option>
<option value=18>18</option>
<option value=19>19</option>
<option value=20>20</option>
<option value=21>21</option>
<option value=22>22</option>
<option value=23>23</option>
<option value=24>24</option>


</select>

<p id="Clockresult">0AM</p>

<script>




function myFunction() {

var value = document.getElementById('mySelect').value;

if (value > 12) {
value = value - 12;
document.getElementById('Clockresult').innerHTML = value + "PM";
} else {
document.getElementById('Clockresult').innerHTML = value + "AM";
}
}

</script>
</body>
</html>

关于javascript - 如何根据输入的小时添加 PM 或 AM?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59020653/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com