简体   繁体   English

React.js 控制 select 选项(下拉) select 改变值

[英]React.js controlled select option (drop-down) select the changed value

I have the following code but when i change the value from drop-down the drop-down sticks to Select the status Rather changing it to the selected value ie我有以下代码,但是当我从下拉列表中更改值时,下拉列表会显示为Select the status而是将其更改为所选值,即

const [formData, setFormData] = useState({
    title: "",
    status: "",
    priority: "",
    start_date: new Date(),
    due_date: new Date(),
    progress: 0,
    user_id: null,
  });

const task_statuses = ["active", "pending", "inactive", "completed"];

  const setData = (e) => {
    setFormData({ ...formData, [e.target.name]: e.target.value });
  };
    <select onChange={setData} name="status">
      <option value=""> Select Task Status </option>
        {task_statuses.map((status) => {
          return (
            <option key={shortid.generate()} value={status}>
              {capitalizeFirstLetter(status)}
            </option>
          );
        })}
      </select>

I would like it to change it to selected option, can't do it please help我想把它改成选中的选项,做不到请帮忙

Please Help请帮忙

Because you are not setting value prop of select tag因为您没有设置 select 标签的 value 属性

Add value prop to value={formData.priority}将 value 属性添加到value={formData.priority}

<select onChange={setData} value={formData.priority} name="status">

Thanks谢谢

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

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