小编mes*_*ill的帖子

使用 javascript 动态更改 <select> 选项

我可以使用 javascript if/else 函数来更改表单的选择选项吗?我不想使用 CSS 来隐藏/显示不同的下拉菜单,所以这是我想出的:

function getType() {
  var x = document.getElementById("food").value;
  var items;

  if (x === "fruit") {
    items = "Apple" || items = "Oranges" || items = "Bananas";
  else {
    items = "Eggplants" || items = "Olives"
  }
  document.getElementById("pickone").value;
}
 
Run Code Online (Sandbox Code Playgroud)
<input type="text" id="food">

 <select id="pickone">
   <option id="1"></option>
   <option id="2"></option>
 </select>
Run Code Online (Sandbox Code Playgroud)

我似乎找不到任何有关如何执行此操作的文档,因此任何帮助都会很棒。

javascript select if-statement

11
推荐指数
2
解决办法
2万
查看次数

每个带有await 的ECMAScript 运行时都会等待任何Thenable 吗?

目前登录node、chrome、firefox的如下Yahtzee

正如你所看到的,连 Promise 的原型都还没有确定。

const fake = new Number(1)
fake.then = fn => setTimeout(fn, 0, 'Yahtzee')
const main = async () => {
  console.log(await fake)
}
main()
Run Code Online (Sandbox Code Playgroud)

这是否普遍有效?更重要的是,这种行为可能会持续下去吗?

javascript async-await es6-promise

5
推荐指数
1
解决办法
423
查看次数

显示所有承诺的订单结果

我是新的承诺.

我遇到了一个需要循环执行一系列任务并获取数据的情况.出于某种原因,我必须按顺序进行,并且必须使用promises来完成.但令我惊讶的是,结果只有最后一个元素而不是所有元素.

代码的简化版本

const _ = require('lodash');

const test = (name) => {
  return new Promise(function(resolve, reject) {
    //This is simplified from my actual code
    resolve(name);
  });
};

const testAll = (arr) => {
  //This chains and returns a series of functions with the order of arr, so that the can be executed in the order, not simultaneously
  let functions = _.map(arr, element => test.bind(null, element));
  //This reduces the functions and is supposed to return an array of all values …
Run Code Online (Sandbox Code Playgroud)

javascript promise lodash

3
推荐指数
1
解决办法
46
查看次数

隐藏的元素不会复制到剪贴板

我试图添加按钮来复制简单的文本字符串,但没有成功.

function kopiraj() {
  var copyText = document.getElementById("toCopy");
  copyText.select();
  document.execCommand("Copy");
  document.getElementById("telefon").innerHTML = 'Copied';
}
Run Code Online (Sandbox Code Playgroud)
<button type="button" onclick="kopiraj()">Copy</button>
<input type="hidden" id="toCopy" value="123456789">
<p id="telefon"></p>
Run Code Online (Sandbox Code Playgroud)

它没有在剪贴板中放任何东西.

我不需要输入字段.我可以在JS本身添加文本.

javascript jquery

3
推荐指数
2
解决办法
2150
查看次数

无法取消选中复选框 (HTML)

我正在使用 JavaScript 处理表单的自动完成功能,并且在我的 HTML 文件中的复选框中遇到了问题。基本思想是用户应该能够在表单字段集中填写他们的送货名称和邮政编码,然后如果他们选中“账单信息是否相同?”,将调用一个 JS 函数来填写账单具有相同运费的帐单名称和邮政编码的字段集。该复选框能够完成此操作,但我无法取消选中它以清除帐单信息。本质上,我无法取消选中我的复选框。我的代码如下:

function billingFunction(){
  var billName=document.getElementById("billingName");
  var shipName=document.getElementById("shippingName");
  var billZip=document.getElementById("billingZip");
  var shipZip=document.getElementById("shippingZip");
  var same=document.getElementById("same");

  if (same.checked=true){
    billName.value=shipName.value;
    billZip.value=shipZip.value;
  }
  else{
    billName.value="";
    billZip.value="";
  }
}
Run Code Online (Sandbox Code Playgroud)
<form>
    <fieldset>
        <legend>Shipping Information</legend>
        <label for ="shippingName">Name:</label>
        <input type = "text" name = "shipName" id = "shippingName" required><br/>
        <label for = "shippingZip">Zip code:</label>
        <input type = "text" name = "shipZip" id = "shippingZip" pattern = "[0-9]{5}" required><br/>
    </fieldset>

    <input type="checkbox" id="same" name="same" onchange= "billingFunction()"/>
    <label for = "same">Is the Billing Information …
Run Code Online (Sandbox Code Playgroud)

html javascript checkbox

2
推荐指数
1
解决办法
4354
查看次数