0

无论如何,在 jQuery datepicker 中选择日期后,我可以继续专注于输入元素吗?还有无论如何我可以阻止用户输入任何内容,但启用 tab 键?我目前正在使用阻止默认方法,但我只需要启用 tab 键。谢谢你。这是我目前的方法。

$('body').on('focus',".dateSem",function() {
    $(this)
      .datepicker({
        changeMonth: true,
        changeYear: true,
        changeDay: true,
        showButtonPanel: true,
        dateFormat: 'yy/MM/dd',
        showMonthAfterYear: true,
        monthNames: ["01", "02", "03", "04",
          "05", "06", "07", "08", "09", "10",
          "11", "12"
        ],
        monthNamesShort: ["1", "2", "3", "4",
          "5", "6", "7", "8", "9", "10",
          "11", "12"
        ],
        dayNamesMin: ["日", "月", "火", "水", "木",
          "金", "土"
        ],
        minDate: new Date,
        currentText: '今日を選択',
        closeText: '確定',
        onClose: function(dateText, inst) {
          $(this).datepicker(
            'setDate',
            new Date(inst.selectedYear,
              inst.selectedMonth,
              inst.selectedDay));
        }
      });
    $(this).keydown(function(e) {
      e.preventDefault();
    });

  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

4

1 回答 1

1

我用下面的 html 做了一个简单的实现(请不要忘记包含 jquery 和 jqueryui)

<label>Date pick 1</label>
<input type="text" class="mydatepicker" />
<br />
<label>Test focus</label>
<input type="text" /><br />
<label>Date pick 2</label>
<input type="text" class="mydatepicker" />
<br />
<label>Simple text to check tab focus</label>
<input type="text" />

和javascript代码

$('.mydatepicker')
 .keydown(function(e){
  if(e.keyCode==9){
     return true;
  }
  return false;
})
.datepicker({
  onSelect:function(date){
   $(this).focus();
  }
});

和解释

您选择所有具有类的输入字段.mydatepicker并分配一个 onkeydown 事件,在该事件中禁用所有键输入,除非键码等于 9(这意味着等于选项卡)

然后你将它们也转换为日期选择器和 onSelect 函数内部(在用户选择日期之后运行),你调用$(this).focus()where thisis the current input element

于 2018-09-07T08:57:52.990 回答