<small id='LGiNs'></small><noframes id='LGiNs'>

    1. <tfoot id='LGiNs'></tfoot>

      <legend id='LGiNs'><style id='LGiNs'><dir id='LGiNs'><q id='LGiNs'></q></dir></style></legend>
      • <bdo id='LGiNs'></bdo><ul id='LGiNs'></ul>

      <i id='LGiNs'><tr id='LGiNs'><dt id='LGiNs'><q id='LGiNs'><span id='LGiNs'><b id='LGiNs'><form id='LGiNs'><ins id='LGiNs'></ins><ul id='LGiNs'></ul><sub id='LGiNs'></sub></form><legend id='LGiNs'></legend><bdo id='LGiNs'><pre id='LGiNs'><center id='LGiNs'></center></pre></bdo></b><th id='LGiNs'></th></span></q></dt></tr></i><div id='LGiNs'><tfoot id='LGiNs'></tfoot><dl id='LGiNs'><fieldset id='LGiNs'></fieldset></dl></div>

      HTML + React:单选按钮卡在最初设置为“已选中"的按钮上.

      HTML + React: Radio button stuck on the one initially set as quot;checkedquot;(HTML + React:单选按钮卡在最初设置为“已选中的按钮上.)

      1. <small id='JqPld'></small><noframes id='JqPld'>

              <tbody id='JqPld'></tbody>

            • <i id='JqPld'><tr id='JqPld'><dt id='JqPld'><q id='JqPld'><span id='JqPld'><b id='JqPld'><form id='JqPld'><ins id='JqPld'></ins><ul id='JqPld'></ul><sub id='JqPld'></sub></form><legend id='JqPld'></legend><bdo id='JqPld'><pre id='JqPld'><center id='JqPld'></center></pre></bdo></b><th id='JqPld'></th></span></q></dt></tr></i><div id='JqPld'><tfoot id='JqPld'></tfoot><dl id='JqPld'><fieldset id='JqPld'></fieldset></dl></div>
                <bdo id='JqPld'></bdo><ul id='JqPld'></ul>
                <legend id='JqPld'><style id='JqPld'><dir id='JqPld'><q id='JqPld'></q></dir></style></legend>
                <tfoot id='JqPld'></tfoot>
              • 本文介绍了HTML + React:单选按钮卡在最初设置为“已选中"的按钮上.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一组两个单选按钮,其中一个在页面加载时被选中.看来我无法将其更改为第二个.如果重要的话,HTML 是由 reactjs 构建的.chrome 上的标记如下所示:

                I have a set of two radio buttons one of which is checked when the page is loaded. It appears that I can't change it to the second one. The HTML is build by reactjs if that matters. The markup on chrome looks like:

                <fieldset data-reactid=".0.0.0.0.0.0.1">
                <label for="coop-radio-btn" data-reactid=".0.0.0.0.0.0.1.0">
                    <span data-reactid=".0.0.0.0.0.0.1.0.0">To a Cooperative</span>
                    <input type="radio" name="requestOnBehalfOf" value="coop" id="coop-radio-btn" data-reactid=".0.0.0.0.0.0.1.0.1">
                </label>
                <label for="union-radio-btn" data-reactid=".0.0.0.0.0.0.1.1">
                    <span data-reactid=".0.0.0.0.0.0.1.1.0">Additional Request</span>
                    <input type="radio" name="requestOnBehalfOf" value="union" checked="" id="union-radio-btn" data-reactid=".0.0.0.0.0.0.1.1.1">
                </label>
                </fieldset>
                

                推荐答案

                这是因为 React 正在制作 input 的受控组件,因为每个输入的值都是从服务器端设置的.

                The reason for this was that React was making the inputs controlled components because the values for each input are set from the server side.

                引用 布兰登的回答:

                如果您将 value={whatever} 属性添加到输入,使其成为受控组件,那么如果您添加 onChange 处理程序,用于更新 whatever 的值.来自 React 文档:

                If you add a value={whatever} property to the input, which makes it a controlled component, then it is read-only uness you add an onChange handler that updates the value of whatever. From the React docs:

                为什么选择受控组件?

                在 React 中使用诸如 <input> 之类的表单组件带来了编写传统表单 HTML 时所没有的挑战.例如,在 HTML 中:

                Using form components such as <input> in React presents a challenge that is absent when writing traditional form HTML. For example, in HTML:

                <input type="text" name="title" value="Untitled" />
                

                这会渲染一个输入已初始化,其值为Untitled.当用户更新输入时,节点的值 property 会改变.但是,node.getAttribute('value') 仍然会返回初始化时使用的值,Untitled.

                This renders an input initialized with the value, Untitled. When the user updates the input, the node's value property will change. However, node.getAttribute('value') will still return the value used at initialization time, Untitled.

                与 HTML 不同,React 组件必须在任何时间点表示视图的状态,而不仅仅是在初始化时.例如,在 React 中:

                Unlike HTML, React components must represent the state of the view at any point in time and not only at initialization time. For example, in React:

                render: function() {
                  return <input type="text" name="title" value="Untitled" />;
                }
                

                由于此方法描述了任何时间点的视图,因此文本输入的值应始终Untitled.

                Since this method describes the view at any point in time, the value of the text input should always be Untitled.

                我找到的最简单的解决方案是将 onChange={function(){}} 添加到 inputs.

                The simplest solution I found is to add onChange={function(){}} to the inputs.

                这篇关于HTML + React:单选按钮卡在最初设置为“已选中"的按钮上.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
                问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
                Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
                quot;Each child in an array should have a unique key propquot; only on first time render of page(“数组中的每个孩子都应该有一个唯一的 key prop仅在第一次呈现页面时)
                CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
                Ordinals in words javascript(javascript中的序数)

                <small id='7XSvJ'></small><noframes id='7XSvJ'>

                <legend id='7XSvJ'><style id='7XSvJ'><dir id='7XSvJ'><q id='7XSvJ'></q></dir></style></legend>

                • <i id='7XSvJ'><tr id='7XSvJ'><dt id='7XSvJ'><q id='7XSvJ'><span id='7XSvJ'><b id='7XSvJ'><form id='7XSvJ'><ins id='7XSvJ'></ins><ul id='7XSvJ'></ul><sub id='7XSvJ'></sub></form><legend id='7XSvJ'></legend><bdo id='7XSvJ'><pre id='7XSvJ'><center id='7XSvJ'></center></pre></bdo></b><th id='7XSvJ'></th></span></q></dt></tr></i><div id='7XSvJ'><tfoot id='7XSvJ'></tfoot><dl id='7XSvJ'><fieldset id='7XSvJ'></fieldset></dl></div>
                    <bdo id='7XSvJ'></bdo><ul id='7XSvJ'></ul>
                        <tbody id='7XSvJ'></tbody>
                        <tfoot id='7XSvJ'></tfoot>