Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/429.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/78.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变量作用域混淆_Javascript_Html_Scope - Fatal编程技术网

与Javascript变量作用域混淆

与Javascript变量作用域混淆,javascript,html,scope,Javascript,Html,Scope,我正在尝试用JS创建一个简单的测试。我有一个函数,可以创建4个单选按钮并将它们添加到html中。当我多次调用n时,我只需在div中添加相同的4个单选按钮,当我尝试从第四个问题中选择一个答案时,例如,它仍然从第一个div/问题中选择答案 函数getRandom(最小值、最大值){ 返回Math.random()*(max-min)+min; } 功能测试_2(){ VarA=getRandom(0,10)-5; while(a==0) a=getRandom(0,10)-5; VarB=getR

我正在尝试用JS创建一个简单的测试。我有一个函数,可以创建4个单选按钮并将它们添加到html中。当我多次调用
n
时,我只需在
div
中添加相同的4个单选按钮,当我尝试从第四个问题中选择一个答案时,例如,它仍然从第一个div/问题中选择答案

函数getRandom(最小值、最大值){ 返回Math.random()*(max-min)+min; } 功能测试_2(){ VarA=getRandom(0,10)-5; while(a==0) a=getRandom(0,10)-5; VarB=getRandom(0,10)-5; //创建添加单选按钮的div var div=document.createElement('div'); div.setAttribute('class','parent'); div.setAttribute('id','2'); 文件.正文.附件(div); //创建单选按钮并设置其属性 var radio1=document.createElement('input'); var label1=document.createElement('label'); label1.innerHTML=a*4+b; label1.setAttribute('for','radio1'); radio1.setAttribute('type','radio'); radio1.setAttribute('value',a*4+b); radio1.setAttribute('id','radio1'); radio1.setAttribute('name','answ'); radio1.innerHTML=a*4+b; //将其添加到div 儿童部(无线电1); 第1部分(标签1); }
测试_2()每个问题的名称属性必须不同:

    <form>
    <!-- Question 01 -->
    <fieldset id="group1">
        <input type="radio" value="" name="group1">
        <input type="radio" value="" name="group1">
    </fieldset>

    <fieldset id="group2">
    <!-- Question 02 -->
        <input type="radio" value="" name="group2">
        <input type="radio" value="" name="group2">
        <input type="radio" value="" name="group2">
    </fieldset>
</form>

这可能是因为生成的每个“单选按钮包”
test_2()
都具有相同的
名称
属性值(
answ
)。 一个快速的解决方案是将
名称
作为参数发送到
test2
,这样您就可以像这样“命名”单选按钮:

function test_2(name) {

  var a = getRandom(0, 10) - 5;

  ...

  radio1.setAttribute('name', name + '-answ');

  ...
}


这样,如果在循环中调用此函数,可以将索引作为name参数传递,并确保名称的唯一性。

请简化代码示例,仅包含问题的相关部分。所有
div
都可以使用
输入[type=radio]
元素被赋予相同的
id
值<代码>ids在文档中必须是唯一的。@代码不需要简化。这里没有多余的东西。相反,应该添加更多相关代码,以便我们可以重新创建场景。没有足够的代码可以确定,但我怀疑问题在于
id
属性共享值,而它们必须是唯一的。是的,我认为id就是问题所在。谢谢大家