首页 \ 问答 \ 如何选择datalist的描述(How to select datalist's description)

如何选择datalist的描述(How to select datalist's description)

我在表单中有2个输入。 在第一个输入中,我通过JSON加载一个datalist 。 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>

我用来加载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();

I have 2 inputs in a form. In the 1st input, I load a datalist through JSON. The json list has 2 attributes machine and lot.

What I want is when the user select a machine, the 2nd input to be filled automatically with the lot. For example If I select 1 the second input to be filled with lot1.

An another approach is to select data-description element. But neither I don't know how..

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>

The actual code I use to load JSON is this:

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();

原文:https://stackoverflow.com/questions/40130036
更新时间:2019-11-06 04:23

最满意答案

至少这个技巧可以提前知道媒体播放是否需要用户手势:

function mediaPlaybackRequiresUserGesture() { 
  var audio = document.createElement('audio');
  audio.play();
  return audio.paused;
}

在这里找到。


At least this trick allows to know in advance if media playback requires user gesture:

function mediaPlaybackRequiresUserGesture() { 
  var audio = document.createElement('audio');
  audio.play();
  return audio.paused;
}

Found here.

相关问答

更多
  • 至少这个技巧可以提前知道媒体播放是否需要用户手势: function mediaPlaybackRequiresUserGesture() { var audio = document.createElement('audio'); audio.play(); return audio.paused; } 在这里找到。 At least this trick allows to know in advance if media playback requires user gesture: ...
  • 如果任何人仍在寻找解决方案,我在Android中发现了如何播放铃声/闹铃的答案 try { Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification); r.play(); } catch (Exception e) ...
  • 简单地将下面的代码放在块中,当发生通知时将触发该代码。 mMediaPlayer = new MediaPlayer(); mMediaPlayer = MediaPlayer.create(this, R.raw.mySound); mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mMediaPlayer.setLooping(true); // Set false if you don't want it to loop mMed ...
  • 以前的代码缺少什么? Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); builder.setSound(alarmSound); What was missing from my previous code: Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); builder. ...
  • 创建一个Audio对象并play它! Audio接受构造函数的链接可能来自任何来源。 var audio = new Audio('audio_file.mp3'); audio.play() Create an Audio object and play it! The link Audio takes in the constructor may be from any source. var audio = new Audio('audio_file.mp3'); audio.play()
  • MMMMhhhhhhkay我做到了。 我在这里发布解决方案,万一有人需要它(我怀疑它,看到答案的数量)。 事情是在主要功能我不得不使用“加载”功能来准备播放声音。 在主要内容中我添加了类似:obsound = document.getElementById('obsound'); obsound.load(); 然后我在计时器结束时调用.play()函数。 MMMMhhhhhhkay I did it. I post the solution here, in case anyone needs it (I ...
  • 之前已经问过您的问题 哪种方法? 您可以使用