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
Monthcontrol i.e.
這個控制項基本上為使用者提供了一個類似日曆的下拉式選單,使用者可以從中選擇或選擇月份和年份。
月份讓我們來看一個簡單的範例,使用
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 firedueen a date valueen a date value. 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()。 ######範例### <pre class='brush:html;toolbar:false;'><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> </pre> ###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 <##gt; tag.###gt; tag.##例子### <pre class='brush:html;toolbar:false;'><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> </pre> ###Once this program is executed, it will show “December, 1980” as the default value in the month control.### ###接下來是非常有趣的部分,那就是設定日期範圍,這意味著使用者只能輸入範圍內的日期值,否則程式會顯示錯誤訊息。 ######範例### <pre class='brush:html;toolbar:false;'><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> </pre> ###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 hs upto Novave be year uple#. ###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 whichwe ####We can also make use of step attribute using whichwe ####We 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### <pre class='brush:html;toolbar:false;'><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> </pre>###</script>
以上是如何在HTML中使用月份輸入類型?的詳細內容。更多資訊請關注PHP中文網其他相關文章!