gpt4 book ai didi

javascript - 在选中单选按钮之前禁用输入字段 (HTML)

转载 作者:技术小花猫 更新时间:2023-10-29 12:39:38 24 4
gpt4 key购买 nike

我有两个字段,一个是文本输入字段,另一个是选择标签。问题是我只想启用其中之一用户应通过单击上面的单选按钮之一来选择启用哪个。

因此,如果用户选择第一个单选按钮,输入字段将被启用如果他选择第二个,选择标签将被启用。

这是我的代码:

        <input type="radio" name="type" value="customurl">wanna custom url?
<input type="text" name="custom" placeholder="should be 5 charecters at least" >
<br><br>
<input type="radio" name="type" value="customurl">random?
<select name="charstype">
<option>Letters</option>
<option>Number</option>
</select>

最佳答案

您将需要使用 javascript。您提供的代码中最短的方法是将 ID 属性附加到选择和输入字段,并通过“onclick”事件禁用/启用它们。

<input onclick="document.getElementById('custom').disabled = false; document.getElementById('charstype').disabled = true;" type="radio" name="type" checked="checked">wanna custom url?
<input type="text" name="custom" id="custom" placeholder="should be 5 charecters at least" >
<br><br>
<input onclick="document.getElementById('custom').disabled = true; document.getElementById('charstype').disabled = false;" type="radio" name="type" value="customurl">random?
<select name="charstype" id="charstype" disabled="disabled">
<option>Letters</option>
<option>Number</option>
</select>

关于javascript - 在选中单选按钮之前禁用输入字段 (HTML),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12491297/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com