首页 > 解决方案 > 如何将变量传递给其他html?

问题描述

我创建了一个测验,我试图计算并计算 10 个不同 html 文件中的分数。我该怎么做?我想要的是分数在正确时添加+1,或者在错误时仅保留并将分数显示为消息,但我遇到了问题,因为它们位于不同的html文件中。

这是我在第一个 html 文件上的脚本:

var g;
var h;
var i;
var j;

function submit001() {
  b = input001.value;
  c = input002.value;
  d = input003.value;
  e = input004.value;
  if (b == "(~ M > Q) ^ (R > ~ T)" || b == "(~M>Q)^(R>~T)") {
    g = 1;
    input001.value = b;
    check001.innerHTML = "<text class=button002>" + "✔" + "</text>";
  } else {
    input001.value = b;
    check001.innerHTML = "<text class=button002>" + "✖" + "</text>";
  }

  if (c == "1, 2, Conj" || c == "1,2,Conj") {
    h = 1
    input002.value = c;
    check002.innerHTML = "<text class=button002>" + "✔" + "</text>";
  } else {
    input002.value = c;
    check002.innerHTML = "<text class=button002>" + "✖" + "</text>";
  }

  if (d == "Q v ~ T" || d == "Qv~T") {
    i = 1;
    input003.value = d;
    check003.innerHTML = "<text class=button002>" + "✔" + "</text>";
  } else {
    input003.value = d;
    check003.innerHTML = "<text class=button002>" + "✖" + "</text>";
  }

  if (e == "3, 4, CD" || e == "3,4,CD") {
    j = 1;
    input004.value = e;
    check004.innerHTML = "<text class=button002>" + "✔" + "</text>";
  } else {
    input004.value = e;
    check004.innerHTML = "<text class=button002>" + "✖" + "</text>";
  }

  if (g == 1 && h == 1 && i == 1 && j == 1) {
    var score1 = 0;
    var score = score1 + 1;
  } else {
    var score = 0;

  }
  message001.innerHTML = "Your score is" + " " + score;
  disappear001.innerHTML = "";
  reload001.innerHTML = "<div id=center001><button class=button001 onclick=next001()>Next</button></div>";
}


function next001() {
  window.location.replace("Question2.html")
}

这是第二个脚本

var p;
var q;
var r;
var s;
var t;
var u;
var v;
var w;

function submit001() {
  x = input001.value;
  y = input002.value;
  z = input003.value;
  a = input004.value;
  b = input005.value;
  c = input006.value;
  d = input007.value;
  e = input008.value;
  if (x == "D ^ W" || x == "D^W") {
    p = 1;
    input001.value = x;
    check001.innerHTML = "<text class=button002>" + "✔" + "</text>";
  } else {
    input001.value = x;
    check001.innerHTML = "<text class=button002>" + "✖" + "</text>";
  }

  if (y == "1, 3, MP" || y == "1,3,MP") {
    q = 1
    input002.value = y;
    check002.innerHTML = "<text class=button002>" + "✔" + "</text>";
  } else {
    input002.value = y;
    check002.innerHTML = "<text class=button002>" + "✖" + "</text>";
  }

  if (z == "D" || z == "D") {
    r = 1;
    input003.value = z;
    check003.innerHTML = "<text class=button002>" + "✔" + "</text>";
  } else {
    input003.value = z;
    check003.innerHTML = "<text class=button002>" + "✖" + "</text>";
  }

  if (a == "4, Simp" || a == "4,Simp") {
    s = 1;
    input004.value = a;
    check004.innerHTML = "<text class=button002>" + "✔" + "</text>";
  } else {
    input004.value = a;
    check004.innerHTML = "<text class=button002>" + "✖" + "</text>";
  }
  if (b == "K" || b == "K") {
    t = 1;
    input005.value = b;
    check005.innerHTML = "<text class=button002>" + "✔" + "</text>";
  } else {
    input005.value = b;
    check005.innerHTML = "<text class=button002>" + "✖" + "</text>";
  }
  if (c == "2, 5, MP" || c == "2,5,MP") {
    u = 1;
    input006.value = c;
    check006.innerHTML = "<text class=button002>" + "✔" + "</text>";
  } else {
    input006.value = c;
    check006.innerHTML = "<text class=button002>" + "✖" + "</text>";
  }

  if (d == "N ^ K" || d == "N^K") {
    v = 1;
    input007.value = d;
    check007.innerHTML = "<text class=button002>" + "✔" + "</text>";
  } else {
    input007.value = d;
    check007.innerHTML = "<text class=button002>" + "✖" + "</text>";
  }
  if (e == "3, 6, Conj" || e == "3,6,Conj") {
    w = 1;
    input008.value = e;
    check008.innerHTML = "<text class=button002>" + "✔" + "</text>";
  } else {
    input008.value = e;
    check008.innerHTML = "<text class=button002>" + "✖" + "</text>";
  }
  if (p == 1 && q == 1 && r == 1 && s == 1 && t == 1 && u == 1 && v == 1 && w == 1) {
    var score1 = score
    var score = score1 + 1;

  } else {
    var score1 = score
    var score = score1 + 0;
  }
  message001.innerHTML = "Your score is" + " " + score;
  disappear001.innerHTML = "";
  reload001.innerHTML = "<div id=center001><button class=button001 onclick=next001()>Next</button></div>";
}


function next001() {
  window.location.replace("Question3.html")
}

标签: javascripthtml

解决方案


您可以使用位置哈希然后访问window.location.hash

window.location.assign('/Question2.html#correct');

然后在脚本二中:

let score = 0;
if (window.location.hash == '#correct') score = 1;

我只是举个例子,但这就是它的工作原理。#表示哈希。我们也使用window.location.assign而不是replace因为它更通用。


推荐阅读