【问题标题】:OnChange function of input type radio in reactjs?reactjs中输入类型radio的onchange函数?
【发布时间】:2018-08-13 15:34:19
【问题描述】:

我有无线电类型的输入标签

               <div className="col-md-12">
                    <div className="form-group clearfix">
                      <label htmlFor="" className="col-md-2 control-label">Authentication</label>
                      <div className="col-md-8">

                        <input type="radio"  name="authentication" value="No Authentication" onChange={this.Authentication.bind(this)} />
                        No Authentication


                      <input type="radio"  name="authentication" value="Master Credentials" onChange={this.Authentication.bind(this)} />
                      Master Credentials


                      </div>
                    </div>
                  </div>

还有onChange函数

Authentication(e) {
this.setState({ authentication: e.target.value });

 }

onChange 仅在我第二次单击时才第一次被调用 onChange 未被调用无法确定问题所在。

【问题讨论】:

    标签: javascript html reactjs


    【解决方案1】:

    添加checked 如下属性:

    <div className="col-md-12">
      <div className="form-group clearfix">
        <label htmlFor="" className="col-md-2 control-label">
          Authentication
        </label>
        <div className="col-md-8">
          <input
            type="radio"
            name="authentication"
            value="No Authentication"
            checked={this.state.authentication === "No Authentication"}
            onChange={this.Authentication.bind(this)}
          />
          No Authentication
          <input
            type="radio"
            name="authentication"
            value="Master Credentials"
            onChange={this.Authentication.bind(this)}
            checked={this.state.authentication === "Master Credentials"}
          />
          Master Credentials
        </div>
      </div>
    </div>;
    

    Demo code.

    【讨论】:

    • 我将在我的答案中添加文档链接,现在似乎官方网站已关闭或其他什么。给 502。
    • 谢谢你。但以前在其他应用程序中我使用相同的方式在那里工作,但我不知道为什么我在这里遇到这个
    猜你喜欢
    • 2020-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-23
    • 2014-02-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多