我的HTML中有两个单选按钮。其中至少有一个值必须为“是”。我想在用户选择第二个值时检查值(如果为“是”,那么我们不在乎,如果为“否”,那么我们必须检查第一个值是什么)。我尝试使用此JavaScript代码,但是它不起作用。 有人可以帮我吗?谢谢。
<div class="form-row">
<div class="form-group col-md-4">
<label class="form-text">Show email.</label>
</div>
<div class="form-group col-md-8">
<div class="btn-group btn-group-toggle" data-toggle="buttons">
<label class="btn btn-secondary active">
<input type="radio" name="email" id="yes" value="yes" autocomplete="off" checked="checked"> Yes
</label>
<label class="btn btn-secondary">
<input type="radio" name="email" id="no" value="no" autocomplete="off"> No
</label>
</div>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-4">
<label class="form-text">Show number.</label>
</div>
<div class="form-group col-md-8">
<div class="btn-group btn-group-toggle" data-toggle="buttons">
<label class="btn btn-secondary active">
<input type="radio" name="number" id="yes" value="yes" autocomplete="off" checked="checked"> Yes
</label>
<label class="btn btn-secondary">
<input type="radio" name="number" id="no" value="no" autocomplete="off" onclick="check(this)"> No
</label>
</div>
</div>
</div>
<script language='javascript' type='text/javascript'>
function check(input) {
if (document.getElementById('email').value == "no" && document.getElementById('number').value == "no")
{
input.setCustomValidity('One must be yes.');
} else {
// input is valid -- reset the error message
input.setCustomValidity('');
}
}
</script>
答案 0 :(得分:0)
我认为类似的方法可能有用:
<label class="btn btn-secondary active">
<input type="radio" name="email" id="firstRadioTrue" value="yes" autocomplete="off" checked="checked"> Yes
</label>
<label class="btn btn-secondary">
<input type="radio" name="email" id="firstRadioFalse" value="no" autocomplete="off"> No
</label>
<label class="btn btn-secondary active">
<input type="radio" name="number" id="secondRadioTrue" value="yes" autocomplete="off" checked="checked"> Yes
</label>
<label class="btn btn-secondary">
<input type="radio" name="number" id="secondRadioFalse" value="no" autocomplete="off"> No
</label>
<script language='javascript' type='text/javascript'>
var radioTopOne = document.getElementById('firstRadioTrue');
var radioTopTwo = document.getElementById('firstRadioFalse');
var radioOne = document.getElementById('secondRadioTrue');
var radioTwo = document.getElementById('secondRadioFalse');
radioOne.addEventListener('click',function(){
if(radioOne.checked == true && radioTopOne.checked == true){
alert("BOTH TRUE");
}
else if(radioOne.checked == true && radioTopOne.checked == false){
alert("TOP FALSE, BOT TRUE");
};
});
radioTwo.addEventListener('click',function(){
if(radioTwo.checked == true && radioTopTwo.checked == true){
alert("BOTH FALSE");
}
else if(radioTwo.checked == true && radioTopTwo.checked == false){
alert("BOT FALSE, TOP TRUE");
}
});
</script>
答案 1 :(得分:0)
当前,您没有带有id
“电子邮件”的元素,也没有带有id
“数字”的元素。您应该在if
语句中为正在使用的两个单选按钮提供一个id
,可以用来检查它们是否已被选中(HTMLInputElement.checked
)。 input
的值即使未选中也将始终相同。
<div class="form-row">
<div class="form-group col-md-4">
<label class="form-text">Show email.</label>
</div>
<div class="form-group col-md-8">
<div class="btn-group btn-group-toggle" data-toggle="buttons">
<label class="btn btn-secondary active">
<input type="radio" name="email" id="showemailyes" value="yes" autocomplete="off" checked="checked"> Yes
</label>
<label class="btn btn-secondary">
<input type="radio" name="email" id="showemailno" value="no" autocomplete="off"> No
</label>
</div>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-4">
<label class="form-text">Show number.</label>
</div>
<div class="form-group col-md-8">
<div class="btn-group btn-group-toggle" data-toggle="buttons">
<label class="btn btn-secondary active">
<input type="radio" name="number" id="shownumberyes" value="yes" autocomplete="off" checked="checked"> Yes
</label>
<label class="btn btn-secondary">
<input type="radio" name="number" id="shownumberno" value="no" autocomplete="off" onclick="check(this)"> No
</label>
</div>
</div>
</div>
<script language='javascript' type='text/javascript'>
function check(input) {
if (document.getElementById('showemailno').checked && document.getElementById('shownumberno').checked)
{
alert("One must be yes.");
input.setCustomValidity('One must be yes.');
} else {
// input is valid -- reset the error message
input.setCustomValidity('');
}
}
</script>