简体   繁体   English

构建月/日下拉选择器,而不是插件

[英]Build a month/day dropdown selector, not a plugin

I have a form, and I would like the user to enter their birth month and birth day. 我有一个表格,我希望用户输入他们的出生月份和出生日期。 However, I can't find a way to build a month and day dropdown selector using jquery. 但是,我找不到使用jquery构建月份和日期下拉选择器的方法。 Everything I read is about a plugin, but I cannot use a plugin. 我阅读的所有内容都是关于插件的,但是我不能使用插件。 Below is the basic html structure, with the "day" select field being dynamically populated, based on what month the user selected. 以下是基本的html结构,其中会根据用户选择的月份动态填充“天”选择字段。

  <select id="month" onchange="selectMonth(this.options[this.selectedIndex].value)" name="month">
       <option value=""></option>
       <option value="January">January</option>
       <option value="February">February</option>
       <option value="March">March</option>
       <option value="April">April</option>
       <option value="May">May</option>
       <option value="June">June</option>
       <option value="July">July</option>
       <option value="August">August</option>
       <option value="September">September</option>
       <option value="October">October</option>
       <option value="November">November</option>
       <option value="December">December</option>
     </select>

<select id="day"></select>

I have created this fiddle to give you an idea of what I am trying to do. 我创建了这个小提琴,以使您了解我正在尝试做的事情。 Unfortunately, this fiddle isn't working (it was the only tutorial I could find to build this, and it didn't work). 不幸的是,这种小提琴无法正常工作(这是我找到的唯一可以构建它的教程,并且无效。) https://jsfiddle.net/katherinekonn/hLc8r915/28/ https://jsfiddle.net/katherinekonn/hLc8r915/28/

Can someone please explain how I would go about creating this? 有人可以解释一下我将如何创建它吗? It is important that based on what month the user selects, the correct number of days will show (for example, is user selects feb, then 29 days will show). 重要的是,根据用户选择的月份,将显示正确的天数(例如,如果用户选择2月,则将显示29天)。

  1. To declare a js function, you may try const selectMonth = month => {} instead of var selectMonth(month){} . 要声明js函数,您可以尝试使用const selectMonth = month => {}而不是var selectMonth(month){}
  2. By looking at your js function, option values should be number instead of the name of the month. 通过查看您的js函数,选项值应该是数字而不是月份的名称。
  3. To clear all options in the drop down list, you may try dateSelect.innerHTML = "" instead of dateSelect.options.length = 0 , which dateSelect.options.length is read only. 要清除下拉列表中的所有选项,可以尝试使用dateSelect.innerHTML = ""代替dateSelect.options.length = 0 ,该dateSelect.options.length是只读的。
  4. To append options in the drop down list, you may try dateSelect.appendChild(new Option(dayNum, dayNum)) instead of dateSelect.options[dateSelect.options.legnth] = new Option(date[month - 1][i], i) 要在下拉列表中附加选项,您可以尝试使用dateSelect.appendChild(new Option(dayNum, dayNum))代替dateSelect.options[dateSelect.options.legnth] = new Option(date[month - 1][i], i)

You may take a look on my solution: https://jsfiddle.net/ymkasf6c/ 您可以看一下我的解决方案: https : //jsfiddle.net/ymkasf6c/

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

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