如何在HTML中使用月份输入类型?

In HTML, a form comprises of various elements which helps in making a user interface in a web page. Using which we can collect different nature of nature.

One of the commonly used control is Month control i.e. ce9cc08b79f89587dcf2dca79f4c8c14

这个控件基本上为用户提供了一个类似日历的下拉菜单,用户可以从中选择或选择月份和年份。 月份 控件允许您以 YYYY-MM 的格式选择日期,其中 YYYY 表示年份,MM 表示月份。

让我们来看一个简单的示例,使用Month控件。

示例

<html>
<body>
   <form name="form1">
      <label for="adm">Date of Admission:</label>
      <input type="month" name="doa">
   </form>
</body>
</html>

Executing the code given above, a month control will be displayed on the page.

When you click on the calendar icon on the right side of the control, it will open the complete month calendar like this −

一旦下拉菜单打开,您可以从日历中选择月份和年份,或者您可以在控件中输入月份和年份。

Once the month and year is selected, it will get stored in String type of value.

Let us create a program to display the selected month and year from the control using JavaScript.

Example

<html>
<head>
   <title>Admission Form</title>
   <script>
      function display(){
         d=form1.doa.value;
         document.write("<center><b>Date of Admission is "+d 
         +"</b></center>");
      }
   </script>
   <form name="form1">
 	   <label for="adm">Date of Admission:</label>
	   <input type="month" name="doa" onchange="display()">
   </form>
<html>

In this program, we have used <script> tag to write JavaScript code in which a function is created to display the selected month when onchange() event is triggered. onchange event will be fired when a date value is selected from the control. document.write() will display the value on the next page. We have displayed the date of admission in the formatted output by using <HTML> formatting tags.

It is displaying date value in YYYY-MM format.

我们还可以在点击提交按钮时显示输出。在这种情况下,我们将使用提交按钮的onclick()事件,而不是onchange()。

示例

<html>
<head>
   <title>Admission Form</title>
   <script>
      function display(){
         d=form1.doa.value;
         document.write("<center><b>Date of Admission is "+d
         +"</b></center>");
      }
   </script>
   <form name="form1">
      <label for="adm">Date of Admission:</label>
      <input type="month" name="doa">
      <br>
      <input type="submit" value="Submit" onclick="display()">
   </form>
<html>

We can also set the default value of month and year which should be displayed when the web page gets loaded in the browser. To do so, we can use value attribute in <input> tag.

Example

<html>
<body>
   <form>
      <p>
         Select a month: <input type="month" name="selectedmonth"  value="1980-12">
         <br>
         <input type="submit" value="Send data">
      </p>
   </form>
</body>
</html>

Once this program is executed, it will show “December, 1980” as the default value in the month control.

接下来是非常有趣的部分,那就是设置日期范围,这意味着用户只能输入范围内的日期值,否则程序将显示错误消息。

示例

<html>
<body>
   <form>
      <p>
         Select a month: <input type="month" name="selectedmonth"       value="1980-12" min="1980-12" max="1981-11">
         <br>
         <input type="submit" value="Send data">
      </p>
   </form>
</body>
</html>

In this program, you can observe that only December month (year 1980) is active and others are disabled and similarly when you type the year 1981, it will show you months upto November because we have set the range.

If user types the invalid month or year name, it will show an error message like this −

We can also make use of step attribute using which we can skip few months for the given year while setting the date range. Look at the example shown below of an event registration form where assuming that a particular event is falling under specific months.

Example

<html>
<body>
   <form name="form1">
      <table>
      <tr>
         <td><label for="adm">Choose Event :</label></td>
         <td><select name="event">
            <option>Live Concert
            <option>Olympics in Beijing
            <option>World Series(Baseball)
            </select>
         </td>
      </tr>
      <tr>
         <td>
         <label for="adm">Available Event Dates:</label></td>
         <td><input type="month" name="eventdate" step="3"></td>
      </tr>
      <tr>
         <td></td><td><input type="submit" value="Submit"></td>
      </tr>
   </form>
</body>
</html>

以上就是如何在HTML中使用月份输入类型?的详细内容,更多请关注双恒网络其它相关文章!

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!
8. 精力有限,不少源码未能详细测试(解密),不能分辨部分源码是病毒还是误报,所以没有进行任何修改,大家使用前请进行甄别
9.本站默认解压密码为:www.sudo1.com
本站提供的一切软件、教程和内容信息仅限用于学习和研究目的。
不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。
本站信息来自网络收集整理,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑或手机中彻底删除上述内容。
如果您喜欢该程序和内容,请支持正版,购买注册,得到更好的正版服务。
我们非常重视版权问题,如有侵权请邮件与我们联系处理。敬请谅解!

云资源网 » 如何在HTML中使用月份输入类型?

常见问题FAQ

免费下载或者VIP会员专享资源能否直接商用?
本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
提示下载完但解压或打开不了?
最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,若小于网盘提示的容量则是这个原因。这是浏览器下载的bug,建议用百度网盘软件或迅雷下载。 若排除这种情况,可在对应资源底部留言,或 联络我们.。
你们有qq群吗怎么加入?
当然有的,如果你是帝国cms、易优cms、和pbootcms系统的爱好者你可以加入我们的QQ千人交流群https://www.sudo1.com/page-qun.html。
  • 会员数(个)
  • 12334资源数(个)
  •        
  • 资源(G)
  •        
  • 今日下载
  • 1406稳定运行(天)

提供最优质的资源集合

立即查看 了解详情