Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 选中单选按钮3时,选中单选按钮1_Javascript_Html_Radio Button - Fatal编程技术网

Javascript 选中单选按钮3时,选中单选按钮1

Javascript 选中单选按钮3时,选中单选按钮1,javascript,html,radio-button,Javascript,Html,Radio Button,我有这样的html页面 <br>Q1A Making decisions after finding out what others think <br> <input name="q1a" id="0" value="0" onclick="MyAlert()" type="radio" required> <input n

我有这样的html页面

<br>Q1A Making decisions after finding out what others think <br>                 
                        <input name="q1a" id="0" value="0" onclick="MyAlert()" type="radio" required>
                        <input name="q1a" id="1" value="1" onclick="MyAlert()" type="radio" required>
                        <input name="q1a" id="2" value="2" onclick="MyAlert()" type="radio" required>
                        <input name="q1a" id="3" value="3" onclick="MyAlert()" type="radio" required>
                        <input name="q1a" id="4" value="4" onclick="MyAlert()" type="radio" required>
                        <input name="q1a" id="5" value="5" onclick="MyAlert()" type="radio" required>

                        <br>Q1B Making decisions without consulting others <br>
                        <input name="q1b" id="6" value="0" onclick="MyAlert()" type="radio" required>
                        <input name="q1b" id="7" value="1" onclick="MyAlert()" type="radio" required>
                        <input name="q1b" id="8" value="2" onclick="MyAlert()" type="radio" required>
                        <input name="q1b" id="9" value="3" onclick="MyAlert()" type="radio" required>
                        <input name="q1b" id="10" value="4" onclick="MyAlert()" type="radio" required>
                        <input name="q1b" id="11" value="5" onclick="MyAlert()" type="radio" required>

Q1A在了解他人想法后做出决策

Q1B在未咨询他人的情况下做出决策
如果用户检查了Q1A的值1,那么此时我想检查Q1B的值4。类比是,Q1A的值+Q1B的值=5

我该怎么做? 我的大致想法是:

<script>
    function MyAlert()  
    {  
      var q1a = document.getElementByName("q1a").value();
      if(q1a == 0){
        $('#5[name="q1b"]').attr('checked', true);
      }
    } 
</script>

函数MyAlert()
{  
var q1a=document.getElementByName(“q1a”).value();
如果(q1a==0){
$('5[name=“q1b”]').attr('checked',true);
}
} 
但这是行不通的

    <a href="https://jsfiddle.net/zLgmc5be/"> My Code Example </a>


谢谢您的帮助。

您的ID重复,无法使用
getElementById()
只返回具有该ID的第一个元素

不是通过元素的ID来查找元素,而是通过它的值来查找元素,这是一个
[value=]
选择器

功能MyAlert(按钮){
如果(button.name==“q19a”){
var otherName='q19b';
var otherVal=5-按钮值;
$(“:radio[name=“+otherName+”][value=“+otherVal+”]”)prop(“选中”,true);
}
}


Q19A运用常识和信念做出决策

Q19B使用数据、分析和理由做出决策

Q20A根据预测提前计划

Q20B在执行计划之前,根据需要进行计划

您有语法错误<代码>文档。getElementById(“q1b”)收音机的语法无效。您能告诉我正确的语法是什么吗?ID必须是唯一的,不能有多个具有相同ID的元素。if语句中存在语法错误,请始终记住使用单个ID。单选按钮必须具有相同的名称,但可以使用ID属性different@VinitaWadhwani现在还不清楚你想用这种语法做什么。单选按钮元素没有
单选
属性。myAlert函数调用未执行place@VinitaWadhwani看到它是一个单选按钮@barmar nota a buttoni它是一个单选按钮。@Vinitawawahwani我在选择器中有
:radio
,有什么问题吗?