gpt4 book ai didi

JavaScript 验证

转载 作者:行者123 更新时间:2023-11-28 10:30:31 28 4
gpt4 key购买 nike

我有两个输入字段holidayDate和Description(id=tags)

<html>
<head>
<script type="text/javascript">
$(document).ready(function() {
$('#holidayDate').datepicker();
var availableTags = ["New years Day", "Martin Luther King Day","Groundhog Day", "Valentine's Day", "Washington's Birthday",
"Easter", "Earth Day", "National Arbor Day", "Mother's Day", "Memorial Day", "Flag Day", "Father's Day", "Independence Day",
"Labor Day", "Columbus Day", "Halloween", "Veterans Day", "Thanksgiving Day", "Pearl Harbor Remembrance Day", "Christmas Day"];
$("#tags").autocomplete({source:availableTags});
$('#holidayDate').change(function() {
var dateString = $(this).val().substring(0, 5);
var res = "";
switch (dateString) {
case '01/01': res = availableTags[0]; break; //If date entered, then return holiday
case '02/02': res = availableTags[2]; break;
case '02/14': res = availableTags[3]; break;
case '04/22': res = availableTags[6]; break;
case '06/14': res = availableTags[10]; break;
case '07/04': res = availableTags[12]; break;
case '10/31': res = availableTags[15]; break;
case '11/11': res = availableTags[16]; break;
case '12/07': res = availableTags[18]; break;
case '12/25': res = availableTags[19]; break;
}
$('#tags').val(res);
});
});
</script>
</head>
<body>
<input id="holidayDate">Date:</input>
<input id="tags">Description:</input>
</body>
</html>

如果我想进行相同的验证,反之亦然,那么我该怎么办?

最佳答案

我建议你将相应的日期放入另一个数组中,如下所示:

var tagDates = [ '01/01', null, '02/02', '02/14', ... ];
/* fill in the rest, with null where you don't have a date */

然后您可以根据需要迭代任一数组。您现有的开关将变成这样:

/* convert date to tag */
for (var i = 0; i < tagDates.length; i++)
if (tagDates[i] == dateString) {
res = availableTags[i];
break;
}

你也可以像这样反过来做:

/* convert tag to date */
for (var i = 0; i < availableTags.length; i++)
if (availableTags[i] == tagString) {
res = tagDates[i];
break;
}

关于JavaScript 验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3239209/

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