繁体   English   中英

需要在Javascript中选择单选按钮

[英]Requiring radio button selection in Javascript

我是JS的初学者,尝试创建一个带有两个选项(左/右)的单选按钮,其中需要选择其中一个选项才能使程序继续,否则它将显示错误屏幕。

我有一些代码可以阻止参与者无论他们按什么就继续(即错误弹出),或者代码允许参与者无论如何继续(即程序继续,即使他们不选择其中一个选项。)我觉得这可能与我的逻辑运算符有关,但我真的不确定。 我尝试使用手动XOR,这似乎不是问题。

我正在使用改编的代码,所以如果还有其他我可以/应该包含的内容,请告诉我!

<div class="radio"><label><input id="option1"  name="option1" type="radio" value="Right"  />Right</label></div>
<div class="radio"><label><input id="option1" name="option1" type="radio" value = "Left" />Left</label></div>

无论如何导致错误的代码:

<input onclick="function filledOut(id) { return (document.getElementById(id).value == 'Left')} if(filledOut('option1') ) { next(); }else{ alert('Please provide a response.'); }" type="button" value="Continue" /> </div>
</div>

导致程序继续的代码:

<input onclick="function filledOut(id) { return ((document.getElementById(id).value == 'Left')|| (document.getElementById(id).value == 'Right'))} if(filledOut('option1') ) { next(); } else{ alert('Please provide a response.'); }" type="button" value="Continue" /> </div>
</div>

您需要将ID更改为不同的ID。 在单选按钮的情况下,“名称”是单选按钮组。 您不需要ID,除非您单独查看每个项目,如果您给它们ID,则需要区别于其他所有ID以及“名称”属性。

https://www.w3schools.com/tags/att_input_type_radio.asp

<input id="optionRight" name="groupName" type="radio" value="Right" /> <input id="optionLeft" name="groupName" type="radio" value="Left" />

此外,您可以将其中一个单选按钮默认设置为选中状态。

如何默认选择一个单选按钮?

<input id="option1" name="groupName" type="radio" value="Right" checked="checked" />

我所理解的是,如果没有检查则需要显示错误,如果选中其中一个则继续。

要做到这一点,您需要检查是否检查了其中任何一个没有检查它的值并给每个单选按钮一个唯一的ID。 你可以做类似的事情

function isChecked(id){//Instead of filledOut
  return document.getElementById(id).checked;
 //.checked returns true or false
}
if (isChecked('option1') || isChecked('option2')){
 next();
}else{
 alert("Your error message")
}

如果需要,另一个获取值的函数:

function getCheckedBtnValue(){
  if(isChecked('option1')) return document.getElementById('option1').value
  if(isChecked('option2')) return document.getElementById('option2').value
  return null
}
//You can also use this function to check if any of them is checked
const value = getCheckedBtnValue();
if(value){
 next();
}else{
 alert("Your error message");
}

另外,尽量不要在HTML元素中编写JavaScript,这可能很难经常阅读。 保留JavaScripting。

<form name="formName">
    <input type="radio" name="option1" id="option1" value="Right"/>Right
    <input type="radio" name="option2" id="option2" value="Left"/>Left
</form>


<input onclick="checkResponse()" type="button" value="Continue" />

checkResponse函数将检查用户单击“继续”按钮时是否选择了任何选项。

<script type="text/javascript">
    function checkResponse(){
        if (isChecked('option1') || isChecked('option2')){
            next();
        }else{
            alert("Your error message")
        }
    }

    function isChecked(id){

     return document.getElementById(id).checked; //returns true if any options are selected

    }

</script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM