如何将无线电输入与文本输入相结合

问题描述 投票:0回答:1

我正在尝试为表单中的“其他”字段构建组合的广播/文本输入。

<input id="artist" type="radio" name="artist" value="Other">
<input id="other-artist" type="text" name="other_artist" placeholder="Other Artist"/>

我无法弄清楚如何在文本输入内部单击并选择了收音机。我尝试将文本输入包装在标签中,但这不起作用。

javascript html css forms radio-button
1个回答
1
投票

你可以添加一个onclick事件来输入并像这样检查收音机。

<input id="artist" type="radio" name="artist" value="Other">

<input id="other-artist" type="text" name="other_artist"placeholder="Other Artist" 
onClick="selectRadio()" />

和js

selectRadio = () => {
     var radio = document.getElementById("artist");
     radio.checked = true;
}

example

希望这个帮助。

© www.soinside.com 2019 - 2024. All rights reserved.