简体   繁体   English

如何设置一个值<select>在不使用 onChange 的情况下使用 React?

[英]How do I set a value for a <select> with React without using onChange?

I got the following HTML select element我得到以下 HTML 选择元素

<select id="bars" onChange={(e) => onBarChange(e)}>
  {props.results.map((bar, y) => (
    <option value={bar.id} key={bar.id} data-key={bar.id}>
      {bar.name} in {bar.city}
    </option>
  ))}
</select>;

with the following onChange method as a callback:使用以下 onChange 方法作为回调:

const onBarChange = (e: React.ChangeEvent<HTMLSelectElement>) => {
  setBarId(parseInt(e.target.value));
};

However, whenever theres only one bar in props.results (props.results.length === 1) then onBarChange(e) will never be called and thus barId will always remain 0 (default select value I guess).但是,只要props.results (props.results.length === 1) 中只有一个栏,那么onBarChange(e)将永远不会被调用,因此barId将始终保持为 0(我猜是默认选择值)。

I tried to put that logic into the submit form like following:我试图将该逻辑放入提交表单中,如下所示:

if(props.results.length === 1) {
  setBarId(props.results[0].id)
}

but that isn't working either.但这也不起作用。 If I console log props.results[0].id I get the correct id but if I console log barId right after this its still 0 again.如果我控制台 log props.results[0].id我得到正确的 id 但如果我控制台 log barId在这之后它仍然是 0。 How can that happen?怎么会这样? Is a rerender triggered and the select immediately sets the default 0 as the state again?是否触发了重新渲染并且选择立即再次将默认值 0 设置为状态?

How can I resolve this so the submit button wrapping this select is sending the correct barId to the backend whenver theres just one bar in props.results (should of course also work with multiple bars..)?我该如何解决这个问题,以便包装此选择的提交按钮在props.results只有一个栏时将正确的 barId 发送到后端(当然也应该与多个栏一起使用..)?

When you are creating barId using useState pass default value.当您使用 useState 传递默认值创建 barId 时。

const [barId, setBarId] = useState(props?.results[0]?.id ?? 0)


const onBarChange = (e: React.ChangeEvent<HTMLSelectElement>) => {
    setBarId(parseInt(e.target.value));
};


<select id="bars" onChange={(e) => onBarChange(e)}>
    {props.results.map((bar, y) => (
        <option value={bar.id} key={bar.id} data-key={bar.id}>
            {bar.name} in {bar.city}
        </option>
    ))}
</select>;

Another thing you can do is add an empty value first.您可以做的另一件事是先添加一个空值。

<select id="bars" onChange={(e) => onBarChange(e)}>
    <option>Select Bar</option>
    {props.results.map((bar, y) => (
        <option value={bar.id} key={bar.id} data-key={bar.id}>
            {bar.name} in {bar.city}
        </option>
    ))}
</select>;

暂无
暂无

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

相关问题 如何在表单的选择字段onChange中传递对象值(使用React,不使用Redux) - How to pass an object value in a form's select field onChange (using React, without Redux) 如何在不使用 onchange 方法的情况下获取输入字段的值 - how can i get the value of an input field in react without using the onchange method 反应-如何设置下拉菜单onChange()的状态? - React - How do a I set the Dropdown Menu state onChange()? 如何使用反应钩子等待 onChange 事件设置值? - How to wait for onChange event to set Value using react hooks? 如何设置 a 的默认值<select>React 中的元素? - How do I set the default value of a <select> element in React? onChange 之前 Select 的设置值将如何执行? - How set value of Select before onChange will execute? 如何在 React 中使用带有钩子的 onChange 更新数组元素? - How do I update elements of array using onChange with hooks in React? 如何在不使用jQuery的情况下设置选择选项值? - How can I set a select option value without using jQuery? 如何为.onchange函数设置默认值,并且仍然保留“ onchange”功能? - How do I set a default value for my .onchange function and still keep the 'onchange' functionality? 使用Javascript,HTML,JQuery和CSS,如何根据HTML设置变量?选择OnChange,可以立即进行计算和更新吗? - Using Javascript, HTML, JQuery & CSS how can I set a variable depending on a HTML Select OnChange, do calculations and update instantly?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM