簡體   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