javascript - 根据onclick事件反应表的位置获取输入字段内部html
问题描述
我并尝试根据单击按钮的输入字段获取输入字段的内部 html 值,如下所示。
<input id="newanswer" placeholder="Create New Answer Here"></input>
我希望能够获得该值并将其作为参数添加到下面的函数中
<button
className="ui negative basic button"
onClick={() => onClick(questionid\\I want the input innerhtml here\\)}
>Create New Answer</button>
The component that renders the table below.
import React, { useState } from 'react';
import { Button } from 'semantic-ui-react';
import { currentloginid } from '../login/loginid.js';
import { deletequestion } from '../question/deletequestion.js';
import { createanswer } from '../answer/createanswer.js';
import {deleteanswer } from '../answer/deleteanswer.js';
export const ViewQuestionComponent = () => {
let [state, setState] = useState([]);
const handleViewQuestion = async () => {
try {
const response = await fetch('http://localhost/gotaquestion/api/api.php?action=viewquestion', {
method: 'GET',
credentials: 'include'
});
const data = await response.json();
const result = await data;
setState(data);
} catch (e) {
console.log(e);
}
}
return (
<div>
<ViewQuestion onClick={handleViewQuestion} />
<div id="questions">
<Table rows={state}>
<DeleteButton onClick={deletequestion} />
<CreateNewAnswerButton onClick={createanswer} />
<DeleteAnswerButton onClick={deleteanswer} />
</Table>
</div>
</div>
);
};
export function ViewQuestion({onClick}) {
return (
<Button onClick={onClick}>View Question</Button>
);
}
export default ViewQuestion;
const Table = ({ rows, setIdTobeDeleted, children }) => (
<table className="ui single line table">
<tbody>
{rows.map((row) => (
<tr key={row.questionid}>
<td>{row.question}</td>
<td>{row.timestamp}</td>
<td>{row.catagories}</td>
<td>{row.answer === null ? "Not Answered" : row.answer}</td>
<td>
{React.cloneElement(children[0], { questionid: row.questionid })}
</td>
<td>
{React.cloneElement(children[1], { questionid: row.questionid })}
</td>
<td>
{React.cloneElement(children[2], { questionid: row.questionid })}
</td>
<td>{row.questionid}</td>
</tr>
))}
</tbody>
</table>
);
const CreateNewAnswerButton = ({ questionid, onClick }) => (
<>
<input id="newanswer" placeholder="Create New Answer Here"></input>
<button
className="ui negative basic button"
onClick={() => onClick(questionid)}
>Create New Answer</button>
</>
);
const DeleteButton = ({ questionid, onClick }) => (
<button
className="ui negative basic button"
onClick={() => onClick(questionid)}
>Delete Question</button>
);
const DeleteAnswerButton = ({ questionid, onClick }) => (
<button
className="ui negative basic button"
onClick={() => onClick(questionid)}
>Delete Answer</button>
);
解决方案
推荐阅读
- python - 根据其他列修改数据框列中的值
- typescript - 如何优雅地初始化 TypeScript 中的一系列对象?
- mysql - char_length() 和 character_length() 有什么区别
- python - 如何获取最大列值计数
- amazon-web-services - 如何通过cloudformation添加密码参数字段而不显示值?
- java - XML 存在时不存在
- c++ - 现在如何获得 gcc3.2?
- python-2.7 - Python2.7上的Shell命令执行
- javascript - 如何在数组中推送地图并清除原始地图以存储更多数据
- flutter - Flutter:如何从 Flutter 中的 ImageProvider 获取 File 对象?