简体   繁体   English

提交表单时不显示单选按钮值

[英]radio button value is not shown when submitting form

I am using redux form for the form elements. 我正在使用redux表单作为表单元素。 The textfield values are shown when submitting but the radio button values is not shown. 提交时显示文本字段值,但不显示单选按钮值。 When i submit the form, i can get the values for email, pass but not role which is a radio button. 当我提交表单时,我可以获取电子邮件的值,通过但不是作为单选按钮的角色。 I have just used {...input} in the input element because it should be suffice, i think. 我认为我刚刚在输入元素中使用了{...input} ,因为它应该足够了。

Here is how i have done 这就是我的表现

radio.js radio.js

const CardToggle = ({ input, meta: { touched, error }, ...props }) => {
  return (
    <React.Fragment>
      <label>
        <StyledInput {...input} type="radio" />
        <StyledBox>
          <CenterElement>{props.children}</CenterElement>
        </StyledBox>
      </label>
      {touched && error && <span>{error}</span>}
    </React.Fragment>
  );
};

export default CardToggle;


submit = (values, mutation) => {
  console.log('values', values);
  // mutation({ variables: values });
};

<styled.Form
  onSubmit={handleSubmit(val => this.submit(val, mutation))}
>
  <Field name="email" label="Email" component={TextField} />
  <Field name="password" label="Password" component={TextField} />
  <AccountType />
</Styled.Form>

<Grid>
  <Field name="role" component={CardToggle} value="enduser">
    <UserIcon size={60} />
    <Title>End User</Title>
  </Field>
  <Field name="role" component={CardToggle} value="company">
    <CompanyIcon size={60} />
    <Title>Company</Title>
  </Field>
</Grid>

Try adding the checked props as well to the radio input field. 尝试将选中的道具也添加到无线电输入字段中。

<StyledInput {...input} type="radio" checked={option.value === input.value}  />

It might be omitting out the checked prop. 它可能会省略已检查的道具。

I am not sure how it worked but it worked when i removed the type="radio" from StyledInput and instead pass the type from the calling component(Field). 我不确定它是如何工作的,但是当我从StyledInput中删除type =“ radio”并从调用component(Field)中传递类型时,它是否起作用。

<Field name="role" type="radio" component={CardToggle} value="enduser">
  <UserIcon size={60} />
  <Title>End User</Title>
</Field>
<label>
  <StyledInput {...input} type={type}/>
    <StyledBox>
      <CenterElement>{props.children}</CenterElement>
    </StyledBox>
</label>

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

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