Javascript HTML单选输入,带有(必需)文本选项;其他";
我有一组无线电输入,带有一个文本字段的选项“other”。我的问题是,当选择其他选项时,如何使文本字段成为必需字段 到目前为止,我提出的代码是:Javascript HTML单选输入,带有(必需)文本选项;其他";,javascript,html,forms,radio-button,Javascript,Html,Forms,Radio Button,我有一组无线电输入,带有一个文本字段的选项“other”。我的问题是,当选择其他选项时,如何使文本字段成为必需字段 到目前为止,我提出的代码是: <form method="POST" action="testPage.php"> <input type="radio" name="option" value="1">Option 1<br> <input type="radio" name="option" value="2">Optio
<form method="POST" action="testPage.php">
<input type="radio" name="option" value="1">Option 1<br>
<input type="radio" name="option" value="2">Option 2<br>
<input type="radio" name="option" value="3">Option 3<br>
<input type="radio" name="option" value="">Other
<input type="text" name="othertext" onchange="changeradioother()">
<input type="submit" value="Submit">
</form>
<script>
function changeradioother(){
var other= document.getElementById("other");
other.value=document.getElementById("inputother").value;
}
</script>
选项1
选项2
选项3
其他
函数changeradioother(){
var other=document.getElementById(“其他”);
other.value=document.getElementById(“inputother”).value;
}
当单击其他单选按钮时,尝试调用函数,
将输入字段设置为所需的
:
函数changeradioother(){
var other=document.getElementById(“其他”);
other.value=document.getElementById(“inputother”).value;
}
函数setRequired(){
document.getElementById(“inputother”).required=true;
}
函数Required(){
if(document.getElementById(“inputother”).required==true){
document.getElementById(“inputother”).required=false;
}
}
选项1
选项2
选项3
其他
这里有很多不同的方法
将id指定给另一个收音机框,并将id指定给文本框
函数更改RadioOther(){
如果(document.getElementById(“其他”)已选中){
if(document.getElementById(“inputtext”).value=''){
警报(“需要输入”);
}
}
返回错误
}
选项1
选项2
选项3
其他
你可以这样做。
检查所有单选按钮以检查单击了哪个
顺便说一下,您没有将id
放入单选按钮Other
var radio=document.querySelectorAll(“输入[name='option']”);
对于(变量i=0;i
选项1
选项2
选项3
其他
你认为这是javascript吗?这是jqueryno,我想这是javascript函数中的jquery代码。请看Op标签。我的意思是人们可以对你的答案进行否决,所以请将其更改为javascript。或者建议你回答说jquery
。很抱歉,我会更正它,谢谢你的提问。当你首先单击“其他”,然后返回到任何其他选项时,它仍然是必需的。@Ivar很好,即使Op从未要求将其更改为正常,但我会更改它。谢谢。它成功了!我喜欢这个,因为它不会打开警报框。谢谢大家的回答