简体   繁体   English

使用多个按钮(几乎(相同))调用JS函数

[英]Multiple buttons to call JS functions with (almost) same arguments

I'm looking at the possibility to create Japanese learning site with some basic interactivity in the form of exercises for the readers. 我正在寻找以读者练习的形式创建具有一些基本交互性的日语学习网站的可能性。 Suppose I have a table like so: 假设我有一个像这样的表:

--------------------------------------------
| characters | definition | roman letters   |
--------------------------------------------
| こんにちは  | hello       | <input type="text">   |
---------------------------------------------
| ...        |            |  <input type="text">  |
--------------------------------------------
|          <grade button> <show answers button> <clear fields button>
------------------------------------------

So the user reads the characters, learns the definition and writes it in the input fields with roman letters. 因此,用户可以读取字符,学习定义并将其写在罗马字母的输入字段中。 When he or she is finished, he can check his answers by pushing the grade button, which will mark the input text in red or green color. 完成操作后,他可以通过按成绩按钮检查答案,该按钮会将输入文本标记为红色或绿色。 Or they can give up and show the answers with the second button, and then clear the answers with the third. 或者他们可以放弃并使用第二个按钮显示答案,然后使用第三个按钮清除答案。

The way this is done now, is that I have an ID for the table and have 3 Javascript functions that does the work described above. 现在完成此操作的方式是,我拥有表的ID,并具有3个执行上述工作的Javascript函数。 The first one takes and ID and an array of correct answers, then sets the colors of all the input fields according to the right answers with JQuery. 第一个使用ID和一组正确答案,然后使用JQuery根据正确答案设置所有输入字段的颜色。 For example, if the ID for the table is #ex1 then I use the selectors #ex1 :text . 例如,如果表的ID是#ex1那么我将使用选择器#ex1 :text The second button also needs an ID and an array of correct answers. 第二个按钮还需要一个ID和一组正确答案。 The third button needs only the ID for the table. 第三个按钮仅需要表的ID。

So in the HTML I for a table I would have something like: 因此,在HTML中,对于表,我将具有以下内容:

<button onclick='grade("ex1", ["some", "array", "with", "answers"])'

<button onclick='showAnswers("ex1", ["some", "array", "with", "answers"])'

<button onclick='clear("ex1")'

This works, but is somewhat ugly because I repeadetly use the same arguments. 这可行,但是有点丑陋,因为我反复使用相同的参数。 So can anyone suggest a better approach? 因此,有人可以提出更好的方法吗?

Edit: I need to have more than one of these per page. 编辑:每页我需要多个。

You can put the answers in a global variable and then you pass it as parameter. 您可以将答案放在全局变量中,然后将其作为参数传递。 Somewhere in your $(document).ready() block you just type 您只需在$(document).ready()块中的某个位置键入

corectAnswers = ["some", "array", "with", "answers"]; 

and call the functions with 并使用

showAnswers("ex1", correctAnswers); 

You may be tempted to use correct answers as the global variable inside of functions and not pass it as a parameter but it's better if you avoid that. 您可能会想将正确答案用作函数内部的全局变量,而不将其作为参数传递,但最好避免这样做。

If you are not using jquery you can put the variables assignment in first block on the page ... 如果您不使用jQuery,则可以将变量赋值放在页面的第一个块中...

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

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