简体   繁体   English

根据html中的输入更改javascript函数

[英]Change a javascript function based on input in html

dataset = data.map(function(d) 
              { return [ +d["Control 1"], +d["Control 2"],
                +d["Exp3"], +d["Exp4"], +d["Exp5"], +d["Exp6"] ]; 
              });

I want to be able to insert other +d's into the function based on user file input (I already have it in an array like ["exp7","exp8"...]). 我希望能够根据用户文件输入将其他+ d插入到函数中(我已经在[[exp7“,” exp8“ ...]这样的数组中添加了它)。 How do I write it so that the array is like +d ["exp7"] and so on and so forth? 我如何编写它,使数组像+ d [“ exp7”]之类,依此类推?

I'm writing this in html. 我用html编写。

Thanks! 谢谢!

I think this would be a good place to make use of currying. 我认为这将是使用Curry的好地方。

You could make a helper that does something like this: 您可以使一个助手执行以下操作:

function t(fields) {
  return function (data) {
    return fields.map(function (field) {
      return +data[field]
    })
  }
}

And then you would make use of it like: 然后您将使用它,例如:

dataset = data.map(t([
  "Control 1",
  "Control 2",
  "Exp3",
  "Exp4",
  "Exp5",
  "Exp6"
]));

This way you can pass in your other arrays of keys in directly. 这样,您可以直接传入其他键数组。

edit: If your operating in an ES6 environment you could use the simple one-line combinator: 编辑:如果您在ES6环境中操作,则可以使用简单的单行组合器:

const t = fs => d => fs.map(f => +d[f])

如何使用 JavaScript 和 HTML 更改特定的背景<div>基于颜色<input> ?</div><div id="text_translate"><p> 问:我正在尝试通过 JS 专门将 div 更改为选定的颜色,而不是整个 body</p><p> 当使用 JS 来利用输入并根据用户决定的颜色更改背景时,我看到/可以工作的唯一代码行是document.body.style.backgroundColor = color;</p><p> 有没有办法,比如说, document.(classNameForADiv).style.backgroundColor = color; ? 我对 HTML、CSS 和 JS 非常陌生,因此我们将不胜感激。</p><p> HTML:</p><pre> &lt;div class="cell text"&gt; Background Color Picker: &lt;/div&gt; &lt;div class="cell js"&gt; &lt;input type="color" id="color_value" name="color_value" value="Black"&gt; &lt;:---DOUBLE (1 COLOR BOX 1 BUTTON)--&gt; &lt;button class="button two" type="button" onclick="changeBackground()"&gt; Set Color: &lt;/button&gt; &lt;/div&gt;</pre><p> JS:</p><pre> function changeBackground(){ let color = document.getElementById('color_value').value; document.body.style.backgroundColor = color; }</pre><p> 这会改变整个 Body 标签的背景,而我正在寻找一种解决方案来改变 Div 标签的背景。</p></div> - How to use JavaScript with HTML to change the background of a specific <div> based on a color <input>?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 HTML如何基于javascript函数的返回值限制文本输入? - How is HTML restricting text input based on the return value of a javascript function? HTML和javascript输入及功能 - HTML and javascript input and function javascript函数(如果输入更改) - javascript function , if input change 基于JavaScript输入的背景更改? - JavaScript Input Based Background Change? 使用带有输入表单的javascript更改html - Change html with javascript with a input form 使用 javascript 根据 html 中的数字输入更改日期输入的值 - Changing value of date input based on change inside a number input in html using javascript Javascript功能更改输入字段 - Javascript function to change input fields 根据本地化和使用javascript更改HTML5输入验证错误文本 - Change HTML5 input validation error text based on localization and using javascript 如何使用 JavaScript 和 HTML 更改特定的背景<div>基于颜色<input> ?</div><div id="text_translate"><p> 问:我正在尝试通过 JS 专门将 div 更改为选定的颜色,而不是整个 body</p><p> 当使用 JS 来利用输入并根据用户决定的颜色更改背景时,我看到/可以工作的唯一代码行是document.body.style.backgroundColor = color;</p><p> 有没有办法,比如说, document.(classNameForADiv).style.backgroundColor = color; ? 我对 HTML、CSS 和 JS 非常陌生,因此我们将不胜感激。</p><p> HTML:</p><pre> &lt;div class="cell text"&gt; Background Color Picker: &lt;/div&gt; &lt;div class="cell js"&gt; &lt;input type="color" id="color_value" name="color_value" value="Black"&gt; &lt;:---DOUBLE (1 COLOR BOX 1 BUTTON)--&gt; &lt;button class="button two" type="button" onclick="changeBackground()"&gt; Set Color: &lt;/button&gt; &lt;/div&gt;</pre><p> JS:</p><pre> function changeBackground(){ let color = document.getElementById('color_value').value; document.body.style.backgroundColor = color; }</pre><p> 这会改变整个 Body 标签的背景,而我正在寻找一种解决方案来改变 Div 标签的背景。</p></div> - How to use JavaScript with HTML to change the background of a specific <div> based on a color <input>? 如何使用Javascript根据HTML中的输入文本字段更改文本元素? - How to change text element based on input text field in HTML using Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM