gpt4 book ai didi

javascript - 将单击按钮的值存储在数组中

转载 作者:行者123 更新时间:2023-11-30 00:08:10 25 4
gpt4 key购买 nike

我有一个测验,在不同的页面上有多个问题,我想将用户的答案存储在一个数组中,以便稍后检查他们的答案是否正确。测验的设置方式是测验的多个部分都有自己的页面,但这些页面上有多个问题。每个问题都依次显示。假设第 1 部分有 2 个问题,单击回答按钮后加载同一页面。问题是这些值没有进入数组。只有一个值存储到数组中,当回答第二个问题时,数组中的第一个值消失,只显示第二个值。 这是我用来将按钮值存储在数组中的代码。

 <script>
$( document ).ready(function() {

var antwoordenObject1= new Array();
$('.btn').click(function() {
antwoordenObject1.push($(this).val());

alert("newArray contents = "+ antwoordenObject1);

});
});
</script>

这是我用来显示问题和多项选择答案的 php 代码:

<?php


$conn = mysqli_connect("127.0.0.1", "root", "", "kinderboekenweek");
$sql = "SELECT * FROM vraag WHERE object_id_object = (SELECT id_object FROM object WHERE speurtocht_speurtocht_id= ".$SpeurtochtID." AND id_object = ".$object.") ";
$resultVragen = $conn->query($sql);

$URL = "";


if ($resultVragen->num_rows > 0) {
while($rowVragen = $resultVragen->fetch_assoc()) {
$arrayVraag[] = $rowVragen['vraag'];
$arrayVraag = array_filter(array_merge(array(0), $arrayVraag));
$arrayAntwoord[] = $rowVragen['antwoord'];
$arrayAntwoord = array_filter(array_merge(array(0), $arrayAntwoord));
$arrayOptie1[] = $rowVragen['optie1'];
$arrayOptie1 = array_filter(array_merge(array(0), $arrayOptie1));
$arrayOptie2[] = $rowVragen['optie2'];
$arrayOptie2 = array_filter(array_merge(array(0), $arrayOptie2));
$arrayOptie3[] = $rowVragen['optie3'];
$arrayOptie3 = array_filter(array_merge(array(0), $arrayOptie3));
$arrayOptie4[] = $rowVragen['optie4'];
$arrayOptie4 = array_filter(array_merge(array(0), $arrayOptie4));
$arrayHint[] = $rowVragen['hint'];
$arrayHint = array_filter(array_merge(array(0), $arrayHint));
}
}


$result = $conn->query("SELECT COUNT(*) FROM vraag WHERE object_id_object = (SELECT id_object FROM object WHERE speurtocht_speurtocht_id= ".$SpeurtochtID." AND id_object = ".$object.") ");
if ($result->num_rows > 0) {
while($rowCount = $result->fetch_assoc()) {
$arrayCount[] = $rowCount;
$arrayCount = array_filter(array_merge(array(0), $arrayCount));
}
}


if($_SESSION['qno'] < implode($arrayCount[1])){
if($_SESSION['qno']>$arrayCount[1] && isset($_SESSION['qno']))
$_SESSION['qno'] = 1;
elseif($_SESSION['qno']<$arrayCount[1] && isset($_POST['vraag1'])){
$_SESSION['qno'] += 1;
unset($_POST['vraag1']);
} elseif ($_SESSION['qno'] != 1 && isset ($_POST['vorige'])){
$_SESSION['qno'] -= 1;
}
}





if($_SESSION['qno'] == implode($arrayCount[1])){
$URL = "loading1.php";
}else {
$URL = "speurtochtvragenpagina.php";
}

?>

<form name="exam" method="post" action="<?php echo $URL; ?>">


<?php echo $arrayVraag[$_SESSION['qno']]; ?> <br>
<input type="hidden" name="vraag1" value="submit" />
<input id = "vraag1" class = 'btn btn-default' type="submit" name="vraag1" value="<?php echo $arrayOptie1[$_SESSION['qno']]; ?> "> <br>
<input id = "vraag1" class = 'btn btn-default' type="submit" name="vraag1" value="<?php echo $arrayOptie2[$_SESSION['qno']]; ?> "> <br>
<input id = "vraag1" class = 'btn btn-default' type="submit" name="vraag1" value="<?php echo $arrayOptie3[$_SESSION['qno']]; ?>"> <br>
<input id = "vraag1" class = 'btn btn-default' type="submit" name="vraag1" value="<?php echo $arrayOptie4[$_SESSION['qno']]; ?>"> <br>

</form>

最佳答案

如果您更改页面,您的 js 数据将被清理。

你必须使用持久化数据。

检查 local-storage .

代码是这样的:

localStorage.setItem("lastname", "Smith");

关于javascript - 将单击按钮的值存储在数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37657924/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com