• React事件处理函数的bind复用和name复用


    一、bind复用

     1 <!DOCTYPE html>
     2 <html lang="zh-cn">
     3 <head>
     4     <meta charset="UTF-8">
     5     <title>表单详解</title>
     6 </head>
     7 <body>
     8     <script src="./react-0.13.2/react-0.13.2/build/react-with-addons.js"></script>
     9     <script src="./react-0.13.2/react-0.13.2/build/JSXTransformer.js"></script>
    10     <script type="text/jsx">
    11         var MyForm = React.createClass({ 
    12             getInitialState: function () {
    13                 return {
    14                     username: "",
    15                     gender: "man",
    16                     checked: true
    17                 }; 
    18             },
    19             handleChange: function (name, event) { 
    20                 var newState = {};
    21                 newState[name] = name == "checked" ? event.target.checked : event.target.value;
    22                 this.setState(newState);
    23             },
    24             submitHandler: function (event) {
    25                 event.preventDefault();
    26                 console.log(this.state); 
    27             },
    28             render: function () {
    29                 return <form onSubmit={this.submitHandler}>
    30                     <label htmlFor="username">请输入用户名:</label>
    31                     <input id="username" type="text" value={this.state.username} onChange={this.handleChange.bind(this, "username")} />
    32                     <br />
    33                     <select value={this.state.gender} onChange={this.handleChange.bind(this, "gender")}> 
    34                         <option value="man"></option>
    35                         <option value="woman"></option>
    36                     </select>
    37                     <br />
    38                     <label htmlFor="checkbox">同意用户协议</label>
    39                     <input id="checkbox" type="checkbox" value="agree" checked={this.state.checked} onChange={this.handleChange.bind(this, "checked")} />
    40                     <button type="submit">注册</button>
    41                 </form>;
    42                 } 
    43             });
    44         React.render(<MyForm></MyForm>, document.body);
    45     </script>
    46 </body>
    47 </html>

    二、name复用

     1 <!DOCTYPE html>
     2 <html lang="zh-cn">
     3 <head>
     4     <meta charset="UTF-8">
     5     <title>表单详解</title>
     6 </head>
     7 <body>
     8     <script src="./react-0.13.2/react-0.13.2/build/react-with-addons.js"></script>
     9     <script src="./react-0.13.2/react-0.13.2/build/JSXTransformer.js"></script>
    10     <script type="text/jsx">
    11         var MyForm = React.createClass({ 
    12             getInitialState: function () {
    13                 return {
    14                     username: "",
    15                     gender: "man",
    16                     checked: true
    17                 }; 
    18             },
    19             handleChange: function (event) { 
    20                 var newState = {};
    21                 newState[event.target.name] = event.target.name == "checked" ? event.target.checked : event.target.value;
    22                 this.setState(newState);
    23             },
    24             submitHandler: function (event) {
    25                 event.preventDefault();
    26                 console.log(this.state); 
    27             },
    28             render: function () {
    29                 return <form onSubmit={this.submitHandler}>
    30                     <label htmlFor="username">请输入用户名:</label>
    31                     <input name="username" id="username" type="text" value={this.state.username} onChange={this.handleChange} />
    32                     <br />
    33                     <select name="gender" value={this.state.gender} onChange={this.handleChange}> 
    34                         <option value="man"></option>
    35                         <option value="woman"></option>
    36                     </select>
    37                     <br />
    38                     <label htmlFor="checkbox">同意用户协议</label>
    39                     <input id="checkbox" name="checked" type="checkbox" value="agree" checked={this.state.checked} onChange={this.handleChange} />
    40                     <button type="submit">注册</button>
    41                 </form>;
    42                 } 
    43             });
    44         React.render(<MyForm></MyForm>, document.body);
    45     </script>
    46 </body>
    47 </html>
  • 相关阅读:
    【BSP视频教程】STM32H7视频教程第4期:从启动到运行过程全解析,电源域,复位,时钟,软硬件启动流程到堆栈,map和htm文件分析(20220126)
    硬核调试实操 | 手把手带你实现 Serverless 断点调试
    兑现 Service Mesh 的新价值:精确控制“爆炸半径”
    宜搭小技巧|自动计算日期时长,3个公式帮你搞定!
    一文看懂边缘云在广电行业的应用
    Java 定时任务技术趋势
    EventBridge 特性介绍|以 IaC 的方式使用 EventBridge
    15M安装包就能玩《原神》,带你了解云游戏背后的技术秘密
    KubeVela v1.3 多集群初体验,轻松管理应用分发和差异化配置
    如何使用 Serverless Devs 部署静态网站到函数计算(上)
  • 原文地址:https://www.cnblogs.com/shamgod/p/5059779.html
Copyright © 2020-2023  润新知