如何在js函数中获取输入值?

时间:2018-04-11 19:15:29

标签: javascript

所以我有这个问题。我有一个用户可以输入的输入。我想在javascript函数中输入。让我演示一下。



function run() {
  // I want the value of #input here
}

<textarea id="input" rows="5" cols="30"></textarea><br><button onclick="run()" id="run">RUN</button>
&#13;
&#13;
&#13;

Disclamer:我不想获取值,我想要它,以便在代码中插入值,就像代码编辑器输入它一样。

4 个答案:

答案 0 :(得分:0)

使用eval()功能,如下所示:

eval(document.getElementById('input').value)

<强>样本

function run() {
  eval(document.getElementById('input').value)
}
<textarea id="input" rows="5" cols="30"></textarea><br><button onclick="run()" id="run">RUN</button>

<强> BUT

注意eval的evilness

答案 1 :(得分:0)

您可以使用eval()方法执行代码段。

import React from "react";
import { render } from "react-dom";

const changeHandler = event => {
  alert(`react slider changed:${event.target.value}`);
};

const App = () => (
  <div>
    <label htmlFor="reactSlider">react-slider</label>
    <input
      id="reactSlider"
      type="range"
      onChange={changeHandler}
      min={10}
      max={100}
      step={10}
      value={20}
    />
    <p>run in the console: $('#reactSlider').val(50).change()</p>
    <br />
    <br />
    <p>Notice that for the React slider, the change event is not triggered.</p>
  </div>
);

render(<App />, document.getElementById("root"));
function run() {
  var text_area = document.getElementById('input').value;
  try {
        eval(text_area);
  }
  catch(error) {
        console.error(error);
  }  
}

答案 2 :(得分:0)

eval替代方案:

CarMax Auto Owner Trust 2018-2
AUTO
https://finsight.com/api/download-csv?file_id=15399
CarMax Auto Owner Trust 2018-2
AUTO
https://finsight.com/api/download-csv?file_id=15395
BENCHMARK 2018-B3 COMMERCIAL MORTGAGE TRUST
CMBS
https://finsight.com/api/download-csv?file_id=15325
BANK 2018-BNK11
CMBS
https://finsight.com/api/download-csv?file_id=15209
Santander Drive Auto Receivables Trust 2018-2
AUTO
https://www.sec.gov/Archives/edgar/data/1383094/000095013118000509/sdart182ex103_0404-1831.xml
Hyundai Auto Receivables Trust 2018-A
AUTO
https://www.sec.gov/Archives/edgar/data/1260125/000114420418019209/tv490265_ex103.xml
BMW Vehicle Owner Trust 2018-A
AUTO
https://www.sec.gov/Archives/edgar/data/1725617/000092963818000431/exhibit103.xml
BMW Vehicle Lease Trust 2017-2
AUTO
https://www.sec.gov/Archives/edgar/data/1716665/000092963818000430/exhibit103.xml
BMWLT 2017-1
AUTO
https://www.sec.gov/Archives/edgar/data/1694920/000092963818000429/exhibit103.xml
GM Financial Consumer Automobile Receivables Trust 2018-2
AUTO
https://www.sec.gov/Archives/edgar/data/1347185/000134718518000012/exh103loanv2.xml
BANK 2017-BNK8
CMBS
https://finsight.com/api/download-csv?file_id=15091
Morgan Stanley Capital I Trust 2017-H1
CMBS
https://finsight.com/api/download-csv?file_id=15087
Morgan Stanley Capital I Trust 2017-HR2
CMBS
https://finsight.com/api/download-csv?file_id=15083
BANK 2017-BNK5
CMBS
https://finsight.com/api/download-csv?file_id=15079
Morgan Stanley Bank of America Merrill Lynch Trust 2017-C33
CMBS
https://finsight.com/api/download-csv?file_id=15075
BANK 2018-BNK10
CMBS
https://finsight.com/api/download-csv?file_id=15059
Wells Fargo Commercial Mortgage Trust 2017-RC1
CMBS
https://finsight.com/api/download-csv?file_id=15055
Wells Fargo Commercial Mortgage Trust 2017-RB1
CMBS
https://finsight.com/api/download-csv?file_id=15051
Wells Fargo Commercial Mortgage Trust 2017-C42
CMBS
https://finsight.com/api/download-csv?file_id=15047
Wells Fargo Commercial Mortgage Trust 2017-C41
CMBS
https://finsight.com/api/download-csv?file_id=15043

答案 3 :(得分:-1)

你试过jquery吗?

$('#run').on('click', function() {
  console.log($('#input').val())
})

https://jsfiddle.net/pigeontoe/6571rzc2/