3 回答

TA贡献1995条经验 获得超2个赞
是的,您忘记在“nein”输入上触发 onclick
<label for="myCheckEd" class="container_radio mr-3">Nein
<input type="radio" name="gehalt" value="nein" class="required" id="nein" onclick="myFunction()">
<span class="checkmark"></span>
</label>

TA贡献1856条经验 获得超17个赞
只需将功能添加到 Nein 收音机即可。
我稍微缩小了代码。
function myFunction() {
var radioJa = document.getElementById("ja");
const text = document.getElementById("Gehalt");
text.style.display = radioJa.checked ? 'block' : 'none';
}
<!DOCTYPE html>
<html>
<body>
<label class="add_top_15">Gehalt (netto)</label>
</br></br>
<div class="form-group radio_input">
<label for="myCheck" class="container_radio mr-3">Ja
<input type="radio" name="gehalt" value="ja" class="required" id="ja" onclick="myFunction()" />
<span class="checkmark"></span>
</label>
<label for="myCheckEd" class="container_radio mr-3">Nein
<input type="radio" name="gehalt" value="nein" class="required" id="nein" onclick="myFunction()" />
<span class="checkmark"></span>
</label>
</div>
</br></br>
<div id="Gehalt" style="display:none" class="form-group add_top_30">
<label for="Gehalt">Gehalt (netto)</label>
<input type="text" name="Gehaltt" id="Gehalt" class="form-control required">
</div>
</body>
</html>

TA贡献1886条经验 获得超2个赞
您可以使用该onchange事件来解决显示问题。
function myFunction(radio) {
var text = document.getElementById("Gehalt");
if (radio.value == "ja"){
text.style.display = "block";
}
else if (radio.value =="nein"){
text.style.display = "none";
}
}
<!DOCTYPE html>
<html>
<body>
<label class="add_top_15">Gehalt (netto)</label>
</br></br>
<div class="form-group radio_input">
<label for="myCheck" class="container_radio mr-3">Ja
<input type="radio" name="gehalt" value="ja" class="required" id="ja" onchange="myFunction(this)">
<span class="checkmark"></span>
</label>
<label for="myCheckEd" class="container_radio mr-3">Nein
<input type="radio" name="gehalt" value="nein" class="required" id="nein" onchange="myFunction(this)">
<span class="checkmark"></span>
</label>
</div>
</br></br>
<div id="Gehalt" style="display:none" class="form-group add_top_30">
<label for="Gehalt">Gehalt (netto)</label>
<input type="text" name="Gehaltt" id="Gehalt" class="form-control required">
</div>
</body>
</html>
添加回答
举报