自动下一次使用datalist聚焦到datepicker

时间:2017-01-23 12:34:37

标签: javascript jquery html css

除第一次输入外,我的表单元素使用自动下一个焦点。我的第一个输入有datalist,当我选择一个列表时,它不会关注next(datepicker)输入。但是,如果我从第二个输入开始,我的自动对焦功能工作得非常好,我该如何处理呢?我也分享了codepen

$(document).ready(function() {

 $('.flexdatalist').flexdatalist({
    minLength: 0,
    searchContain: true,
  }).on('select:flexdatalist', function() {
    $('#checkin').focus();
  });


  var assignPicker = function(id, whenClosed) {
    if (typeof whenClosed !== 'function') {
      whenClosed = null;
    }

    return new Pikaday({
      numberOfMonths: 2,
      field: document.getElementById(id),
      format: "DD.MM.YYYY",
      minDate: new Date(),
      firstDay: 1,
      maxDate: new Date(2020, 12, 31),
      onSelect: function() {
        e = this.getDate();
      },
      onClose: whenClosed
    });
  }

  assignPicker('checkin', function() {
    $('#checkout').focus();
  });
  assignPicker('checkout', function() {
    $('#select').focus();
  });

});
body {
  padding: 30px;
}
input,
select {
  width: 100%;
  padding: 10px;
  border: 1px solid #ccc;
}
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pikaday/1.5.1/css/pikaday.min.css" />
  <link rel="stylesheet" href="http://cdn.anitur.com.tr/example/flexdatalist/flexdatalist.css" />




</head>

<body>


  <div class="container">


    <div class="row">

      <div class="col-lg-3 col-md-3 col-sm-3">
        <input type="text" name="" class='flexdatalist' data-min-length='1' list='languages' name='language' />
        <datalist id="languages">
          <option value="PHP">PHP</option>
          <option value="JavaScript">JavaScript</option>
          <option value="Cobol">Cobol</option>
          <option value="C#">C#</option>
          <option value="C++">C++</option>
          <option value="Java">Java</option>
          <option value="Pascal">Pascal</option>
          <option value="FORTRAN">FORTRAN</option>
          <option value="Lisp">Lisp</option>
          <option value="Swift">Swift</option>
          <option value="ActionScript">ActionScript</option>
        </datalist>
      </div>
      <div class="col-lg-3 col-md-3 col-sm-3">
        <input type="text" class="checkin" id="checkin" />
      </div>
      <div class="col-lg-3 col-md-3 col-sm-3">

        <input type="text" class="checkout" id="checkout" />
      </div>
      <div class="col-lg-3 col-md-3 col-sm-3">

        <select name="select" id="select">
          <option value="0">Choose</option>
          <option value="1">1</option>
          <option value="2">2</option>
          <option value="3">3</option>
          <option value="4">4</option>
          <option value="5">5</option>
          <option value="6">6</option>
          <option value="7">7</option>
        </select>
      </div>

    </div>


  </div>



  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/pikaday/1.5.1/pikaday.min.js"></script>

  <script src="http://cdn.anitur.com.tr/example/flexdatalist/flexdatalist.js"></script>

</body>

</html>

2 个答案:

答案 0 :(得分:1)

我处理了http而不是https上加载文件的所有问题后,大部分代码都还可以。

此解决方案的核心是在onSelect配置中使用pickaday,而不是onClose并在没有assignPicker的情况下定义var函数。< / p>

https://codepen.io/anon/pen/VPWaJo

答案 1 :(得分:0)

更正datalist的输入标签。像这样:

    <form action="" method="get">
    <input  list="languages" name="languages">
      <datalist id="languages">
        <option value="PHP">
        <option value="JavaScript">
        <option value="Cobol">
        <option value="C#">
        <option value="C++">
        <option value="Java">
        <option value="Pascal">
        <option value="FORTRAN">
        <option value="Lisp">
        <option value="ActionScript">
      </datalist>
    </form>