如何将点数分成表格中的答案?

时间:2018-05-27 19:31:34

标签: javascript forms

我知道如何在php中完成它,但在JS中却没有,所以我需要一些帮助才能开始。

我有10个问题,都有10分给a,b,c或d。

1         2         3
a.  3     a. 0      a.  2
b.  1     b. 0      b.  3
c.  5     c. 0      c.  2
d.  1     d. 10     d.  3
等等,所以这一切加起来可分为10分!

问题如下:

Give points to these colors:
[  ] a) green
[  ] b) blue
[  ] c) red
[  ] d) orange
[10] <-- points that still can be divided, and a check V when its good.

格式可以在select,input = number字段中,并不重要,如果所有点都在答案上划分,则必须在按提交时进行检查,因此我可以将其用作$ _POST答案php和我的魔法,所以它不必一遍又一遍地重新加载整个表单。

我真的不知道如何/从哪里开始,因为我无法在网上找到类似的东西。

1 个答案:

答案 0 :(得分:1)

这是一个使用文本框和简单的错误检查。这是你在找什么?

function check() {
  var green = Number(document.forms["myform"]["green"].value);
	var blue = Number(document.forms["myform"]["blue"].value);
	var red = Number(document.forms["myform"]["red"].value);
	var orange = Number(document.forms["myform"]["orange"].value);
	var total = green + blue + red + orange;
	if (total == 10) {
	  return true;
	} else {
	  alert("You have allocated " + total + " points. You must allocate exactly ten.");
		return false;
	}
}
<form name="myform" onsubmit="return check()">
<p>Give points to these colors:</p>
<input name="green" size="2" type="text" /> a) green<br />
<input name="blue" size="2" type="text" /> b) blue<br />
<input name="red" size="2" type="text" /> c) red<br />
<input name="orange" size="2" type="text" /> d) orange<br />
<button type="submit">OK</button>
</form>