html - 选择“否”单选按钮时打开的文本框,必须重新使用才能继续
问题描述
我迫切需要帮助。我有一个是和否单选按钮。否单选按钮需要打开一个隐藏的文本框,需要填写该文本框才能继续表单。我对 html 完全陌生,不知道如何完成这个。
解决方案
例如,它可以这样做:
function submitForm() {
if (document.forms["form"]["no"].checked && document.forms["form"]["noTextarea"].value == "") { // If No option checked and if textarea´s value is empty ("")
alert("If checked No, textarea must be filled.");
return false;
}
else if (document.forms["form"]["yes"].checked == false && document.forms["form"]["no"].checked == false) { // If nothing is checked
alert("Nothing was checked. You must select Yes or No.");
return false;
}
else if (document.forms["form"]["yes"].checked) { // If checked Yes
alert("Form OK.");
}
else { // Else (if checked No and textarea filled
alert("Form OK.");
}
}
function showTextarea() {
if (document.forms["form"]["no"].checked) {
document.forms["form"]["noTextarea"].style.display = "block";
}
else {
document.forms["form"]["noTextarea"].style.display = "none";
}
}
<form id="form" onsubmit="return submitForm()">
<input type=radio name=yesnoRadio id=yes onclick=showTextarea()>
<label for=yes>Yes</label>
<input type=radio name=yesnoRadio id=no onclick=showTextarea()>
<label for=no>No</label>
<br>
<textarea id="noTextarea" style="display: none;" placeholder="Requied when selecting 'No'"></textarea>
<br>
<input type=submit>
</form>
推荐阅读
- python - 当程序在 Python 中循环时,如何从列表中的用户获取多个输入以进行打印?
- javascript - 补间/动画threejs相机到顶视图
- android - 谷歌地图中的地址位置
- c# - 对分区 CosmosDB 集合的查询不一致
- c - 将一个文本文件拆分为多个在 c 中具有特定名称的文本文件
- c# - 当键为结构时,C# 字典性能受到很大影响
- angularjs - 如何像班次一样在电子中加载多个页面
- android - 未获取捕获的图像路径
- python - UnicodeDecodeError:“ascii”编解码器无法解码位置 5 中的字节 0xc3:序数不在范围内(128)
- python - 如何跟踪进程的资源使用情况?