繁体   English   中英

如何使用 react 将 textarea 新行值存储到数组中,并使用 post 请求将其发送到 node.js 变量

[英]how to store textarea new line value into array with react and send it to node js variable with post request

如何使用 react 将 textarea 新行值存储到数组中,并使用 post 请求将其发送到 node.js 变量

const [inputvalue, setInputvalue] = useState("");

const  handleSubmit = (e) => {
  e.preventDefault();
  try {
    axios.post('/inputData', inputvalue , {
      headers: { 'Content-Type': 'text/plain' }
    })
      .then((response) => {
        setData(response.data);
      });
  } catch (e) {
    console.log('Error!', e);
  }
  }
<form onSubmit={handleSubmit}>
      
        <TextField id="outlined-multiline-static" label="Multiline" multiline rows={4} onChange={(e) => setInputvalue(e.target.value)} value={inputvalue} placeholder="Enter your keyword" />
      
      <button type="submit" className="btn btn-primary mt-3" onClick={() => setLoading(true)}>Click here</button>
    </form>

我不建议将输入值保留为数组(因为它会弄乱您的文本区域输出)。 但是,您可以在通过 axois 提交时轻松创建数组。

只需根据您的请求使用“.split(/\n/)”来创建值数组。

你可能想这样做:

axios.post('/inputData', { values: inputvalue.split(/\n/) } , {
  headers: { 'Content-Type': 'text/plain' }
})
.then((response) => {
  setData(response.data);
});

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM