如何选择datalist的描述

时间:2016-10-19 11:22:15

标签: javascript jquery html-datalist

我在表单中有2个输入。在第一个输入中,我加载datalistJSON。 json列表有2个属性machinelot

我想要的是当用户选择一台机器时,第二个输入将自动填充该批次。例如,如果我选择1要填充lot1的第二个输入。

另一种方法是选择data-description元素。但我不知道怎么做..



var dataList = document.getElementById('lots');

var jsonOptions = [{
  "machine": 1,
  "lot": "lot1"
}, {
  "machine": 2,
  "lot": "lot2"
}];

// Loop over the JSON array.
jsonOptions.forEach(function(item) {
  var option = document.createElement('option');
  
  option.value = item.machine;
  option.text = item.lot;
  option.setAttribute('data-description', item.lot);
  dataList.appendChild(option);
});

$(function() {
  $('#machine').on('change keyup', function() {
     var i = this.value;
     
     $('#lot').val(i);
   

  });
});

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

<div class="form-group">
  <div class="col-md-5">
    <input type="text" id="machine" list="lots" class="form-control" name="machine" />
    <datalist id="lots"></datalist>
  </div>
</div>

<br/>

<div class="form-group">
  <div class="col-md-5">
    <input type="text" id="lot" class="form-control" name="lot" />
  </div>
</div>
&#13;
&#13;
&#13;

我用来加载JSON的实际代码是:

var dataList = document.getElementById('lots');
var input = document.getElementById('machine');
var request = new XMLHttpRequest();

request.onreadystatechange = function(response) {
  if (request.readyState === 4) {
    if (request.status === 200) {
      // Parse the JSON
      var jsonOptions = JSON.parse(request.responseText);

      // Loop over the JSON array.
      jsonOptions.forEach(function(item) {
        var option = document.createElement('option');

        option.value =item.machine;
        option.text = item.lot;
        option.setAttribute('data-description', item.lot);
        dataList.appendChild(option);
      });

      // Update the placeholder text.
      input.placeholder = "machine";
    } else {
      // An error occured :(
      input.placeholder = "error:(";
    }
  }
}; 

// Update the placeholder text.
input.placeholder = "Loading options...";

// Set up and make the request.
request.open('GET', 'lots.json', true);
request.send();

2 个答案:

答案 0 :(得分:2)

您需要再次通过JSON解析才能获得lot。请参阅以下代码。

您无需将lot设置为data属性。

var dataList = document.getElementById('lots');

var jsonOptions = [{
  "machine": 1,
  "lot": "lot1"
}, {
  "machine": 2,
  "lot": "lot2"
}];

// Loop over the JSON array.
jsonOptions.forEach(function(item) {
  var option = document.createElement('option');
  
  option.value = item.machine;
  option.text = item.lot;
  dataList.appendChild(option);
});

$(function() {
  $('#machine').on('change keyup', function() {
     var i = this.value;
    var obj = jsonOptions.filter(function(obj){ return obj.machine === parseInt(i)})
     $('#lot').val(obj[0] ? obj[0].lot : '');
   

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

<div class="form-group">
  <div class="col-md-5">
    <input type="text" id="machine" list="lots" class="form-control" name="machine" />
    <datalist id="lots"></datalist>
  </div>
</div>

<br/>

<div class="form-group">
  <div class="col-md-5">
    <input type="text" id="lot" class="form-control" name="lot" />
  </div>
</div>

答案 1 :(得分:2)

$(function() {
  $('#machine').on('change keyup', function() {
     var i = this.value;
    var value ="";
//fetch from the existing options
     jsonOptions.forEach(function(a){
       if(a.machine == i){
         value = a.lot;
       }
     });
     $('#lot').val(value);
  });
});