weixin_33697898 2018-11-12 03:27 采纳率: 0%
浏览 8

阅读表格输入?

How would you read the input value ?

On the reacjs site I see very complicated way !!

https://reactjs.org/docs/forms.html

I just want to read the value and submit it via ajax fetch() request. I.e. I don't need to manage bindings, events and such ...

  • 写回答

2条回答 默认 最新

  • weixin_33726943 2018-11-12 03:42
    关注

    The easiest way by far to read values from html controls is by using an event handler.

    export default class myComponent extends Component {
        person = {};
    
        onChange = field => e => {
          this.person[field] = e.target.value;
        };
    
        render() {
          return (
            <Input
              id="firstName"
              name="firstName"
              autoComplete="firstName"
              autoFocus
              onChange={this.onChange('FirstName')}
            />
          );
        }
      }
    

    In the above code snippet we are basically telling react to fire the onChange member on an update of firstName control update. Our method will receive an event e, that has a handle to our control and we can basically probe it's value member to get what's typed in (much like jquery's $('#element').value()).

    Why is it the easiest method? because it's generic enough to allow you to handle multiple inputs in a react component. Notice that, I'm also instructing React to pass me the control name in addition to the event, and using this method I can basically know exactly which of my inputs (in case of multiple) caused the event to fire.

    评论

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?