【问题标题】:How to get the value of a radio input which is by default checked?如何获取默认选中的无线电输入的值?
【发布时间】:2021-11-05 04:08:37
【问题描述】:

我在.map 方法中有一个单选按钮,第一个被选中defaultChecked={index === 0},我如何在这里获得option 的值?不使用普通的 javascript

const [optionData, setOptionData] = useState({})



{variant.options.map((option, index) => {


<input
                                  type="radio"
                                  name="variant-select"
                                  id="variant-select"
                                  className="focus:ring-0 focus:ring-offset-0 focus:text-red-600 text-red-600 border-none -mt-1 cursor-pointer z-10"
                                  defaultChecked={index === 0}
                                  value={index}
                                  onClick={(e) => {
                                    setOptionData(option)
                                    }
                                  }}
                                />
}
}

【问题讨论】:

  • 你想从哪里获得期权的价值?
  • 对不起,我没听懂你的问题。 @越南
  • how do I get the value of the option herehere 是什么?
  • 这里,我的意思是当它被默认选中时
  • 当用户点击不同的单选按钮时,我可以这样做onClick={(e) =&gt; { setOptionData(option) } }}

标签: javascript reactjs radio-button


【解决方案1】:

这个语法可能对你有帮助:function myFunction() { var x = document.getElementById("myRadio").value; document.getElementById("demo").innerHTML = x; }

【讨论】:

  • 对不起哥们,有什么办法可以通过反应来做到这一点吗?因为有时我会遇到文档未定义之类的问题。
  • 我不明白你在问什么。告诉我更多关于你的问题。
  • 我想在不使用任何点击或更改监听器的情况下获取 defaultChecked 输入的值,我将获得该值,我想将其存储在本地状态中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-28
  • 2020-01-24
  • 2010-09-14
  • 2014-11-15
  • 2011-06-03
  • 2014-06-03
相关资源
最近更新 更多