首页 \ 问答 \ JavaScript无线电输入(JavaScript radio input)

JavaScript无线电输入(JavaScript radio input)

我正在做一个任务,我必须建立一个测验应用程序。 我一次只显示一个问题。 用户必须从一组无线电输入中选择4个答案中的一个。

出于某种原因,它仅在用户选择第一个无线电输入时才有效,其他则无响应。

这是我的代码:

var counter = 0;
var questions = ["<h3>What is the 9 + 10 </h3>\n\
                   <input type='radio' name = 'radio' class='rad' value ='19'>19\n\
                    <input type='radio' name = 'radio' class='rad' value ='23'> 23\n\
                    <input type='radio' name = 'radio' class='rad' value ='44'>66\n\
                    <input type='radio' name = 'radio' class='rad' value ='1'>123 ",
    "<h3>What is the 5 + 10 </h3>\n\
                   <input type='radio' name = 'radio' class='rad' value ='15'>15\n\
                    <input type='radio' name = 'radio' class='rad' value ='23'> 23\n\
                    <input type='radio' name = 'radio' class='rad' value ='44'>44\n\
                    <input type='radio' name = 'radio' class='rad' value ='1'>12 "
];

document.getElementById("question").innerHTML = questions[counter];
document.querySelector('.rad').addEventListener("change", nextQuestion);

function nextQuestion() {
    console.log(counter);
    counter++;
    document.getElementById("question").innerHTML = questions[counter];
}

I am working on an assignment where I have to build a quiz app. I display questions one at a time. The user has to select one of the 4 answers from a group of radio inputs.

For some reason it only works when user selects first radio input, other get no response..

Here is my code :

var counter = 0;
var questions = ["<h3>What is the 9 + 10 </h3>\n\
                   <input type='radio' name = 'radio' class='rad' value ='19'>19\n\
                    <input type='radio' name = 'radio' class='rad' value ='23'> 23\n\
                    <input type='radio' name = 'radio' class='rad' value ='44'>66\n\
                    <input type='radio' name = 'radio' class='rad' value ='1'>123 ",
    "<h3>What is the 5 + 10 </h3>\n\
                   <input type='radio' name = 'radio' class='rad' value ='15'>15\n\
                    <input type='radio' name = 'radio' class='rad' value ='23'> 23\n\
                    <input type='radio' name = 'radio' class='rad' value ='44'>44\n\
                    <input type='radio' name = 'radio' class='rad' value ='1'>12 "
];

document.getElementById("question").innerHTML = questions[counter];
document.querySelector('.rad').addEventListener("change", nextQuestion);

function nextQuestion() {
    console.log(counter);
    counter++;
    document.getElementById("question").innerHTML = questions[counter];
}

原文:https://stackoverflow.com/questions/47879179
更新时间:2022-06-26 15:06

最满意答案

static关键字将name链接到每个包含的翻译单元中,因此每个.cpp文件基本上都有其自己的name版本。

你想要做的是把你想要共享的name放在一个.cpp文件中(没有static文件,并在.h文件中用extern链接声明它)。

所以那是:

a.cpp

namespace settings {
    char name[16] = { 0 };
}

namespace settings {
    extern char name[16];
}

b.cpp

void B::doSomething()
{
    strcpy(settings::name,"I like Dogs");
}

c.cpp

#include "a.h"

void C::printSomething()
{
    printf("Some Girls Say %s\n",settings::name);
}

the static keyword links the name into each translation unit it is included in, so each .cpp file basically has its own version of name.

What you want to do is put the name you want to share in one .cpp file (without the static and declare it with extern linkage in the .h file.

so that's:

a.cpp

namespace settings {
    char name[16] = { 0 };
}

a.h

namespace settings {
    extern char name[16];
}

b.cpp

void B::doSomething()
{
    strcpy(settings::name,"I like Dogs");
}

c.cpp

#include "a.h"

void C::printSomething()
{
    printf("Some Girls Say %s\n",settings::name);
}

相关问答

更多
  • 不,输出是正确的,因为当main方法执行最后一个println ,并不能保证你的线程也已完成执行。 添加tt.join(); 在最后一次印刷之前: tt.join(); System.out .println("at the end of main(), tt.isAlive()=" + tt.isAlive()); 现在主线程将被阻塞,直到tt结束,因此println只有在tt死后才会执行。 至于你的输出总是相同的,10是一个太细的粒度值,并且很可能每个线程在上下文切换时完成所有内容的 ...
  • 当您尝试设置天数时,问题出在PHP代码中。 你有一个if和one if ... else ,但是没有你想象的那样。 如果你有第一个 if($name == 02) { $monthdays = 29; } 在这之后: if($name == 06 || $name == 09 || $name == 04 || $name == 11) { $monthdays = 30; } else { $monthdays = 31; } 最后一个将天数设置为30或31,因为它只有两个选择 ...
  • static关键字将name链接到每个包含的翻译单元中,因此每个.cpp文件基本上都有其自己的name版本。 你想要做的是把你想要共享的name放在一个.cpp文件中(没有static文件,并在.h文件中用extern链接声明它)。 所以那是: a.cpp namespace settings { char name[16] = { 0 }; } 啊 namespace settings { extern char name[16]; } b.cpp void B::doSomethin ...
  • 轻微的错误修复(以及明显的样式更改):您的版本可以多次打印出checked =“checked”。 $cat和$cat_id需要是数组吗? while ( $row = mysqli_fetch_array($results, MYSQLI_ASSOC) ) { $cat = $row['category']; $cat_id = $row['cat_id']; echo '
  • print Dumper(%timeGroups)将%timeGroups平为一个键和值列表,并将多个参数传递给Dumper 。 通常您想要将Dumper传递给单个引用: print Dumper(\%timeGroups) 。 print Dumper(%timeGroups) is flattening %timeGroups into a list of keys and values and passing multiple arguments to Dumper. Usually you want ...
  • 你的代码是正确的。 由于您使用new []进行分配,因此需要使用delete []取消分配。 此外,显然需要以相反的顺序取消分配 - 首先是内部数组,然后是外部数组。 Your code is correct. Since you allocate using new [], de-allocating with delete [] as you do is required. Also, obviously de-allocating in reverse order - inner arrays fir ...
  • 问题是Integer distance = br.read(); 假设br是BufferedReader的实例。 read方法只读取一个字符并给出该字符的整数值。 您可以通过打印distance变量进行验证。 您需要使用readLine并使用Integer.parseInt转换字符串编号 Issue is with Integer distance = br.read(); Assuming br is instance of BufferedReader. read method just reads a ...
  • 谢谢stevieb你让我走在正确的道路上。 这似乎工作: use Nagios::Config; use Nagios::Object::Config; use Data::Dumper; use feature 'say'; use Getopt::Long; Getopt::Long::Configure('bundling'); GetOptions( "C|config=s" => \$OPTION{'config'} ); my $objects; $objects = Nagi ...
  • 您在第二个参数中传递一个对象,该对象包含的内容似乎是基于状态代码的响应处理程序。 jQuery并不是那样做的。 $.get()的第二个参数是可选的data参数,用于将数据传递给服务器。 jQuery序列化此数据并将其作为get请求中的查询字符串包含在内。 您可能想要使用该函数的另一种形式,该函数接受包含url的配置对象: $.get({ url: '/users/test/' + username, statusCode: { 409: () => { vali ...
  • 对于Q3, elev[i]是一个numpy.int32实例,而numpy.int32 elev[i]**4溢出。 对于Q1, elev是Python int,而elev**4使用任意精度算术。 With Q3, elev[i] is a numpy.int32 instance, and elev[i]**4 overflows. With Q1, elev is a Python int, and elev**4 uses arbitrary-precision arithmetic.
  • 相关文章

    更多

    最新问答

    更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)