gpt4 book ai didi

javascript - jQuery 变量未按预期计算

转载 作者:行者123 更新时间:2023-12-03 09:37:44 26 4
gpt4 key购买 nike

我正在创建一个带有数据流的 jQuery 脚本,其中窗口变量用于确定脚本应继续执行的路径。预期的路径是这样的:

  • 初始函数将变量 window.QuestionSelector 设置为 0、-1 或数字
  • window.QuestionSelector 的值为 0 表示全新的父测验,-1 表示新问题,数字表示引用现有问题。

这是调用问题函数的代码

 $(".questionJumpLink").click(function () {
//questionJumpLink will have a data-attribute attached if you're selecting a different question

if ($(this).attr('data-questionid')) {
window.QuestionSelector = $(this).attr('data-questionid');
console.log("Question ID " + window.QuestionSelector + " requested");
}


if (window.QuestionSelector == -1) {
console.log("New question request detected");
}

questionFunction();
}

这个 if 语句是 questionFunction() 的一部分,尽管 window.QuestionSelector 的值为 -1,但仍会被触发。

 //Used when navigating between questions, not on initial load. QuestionSelector
//is the QuestionID being requested. -1 can be set for a new question indicator
if (window.QuestionSelector != 0 && window.QuestionSelector != -1)
{
console.log("Question switch, same quiz path switch");
//Some code and handling, not vital
}

这个 if 语句位于 questionFunction() 中上述​​代码块的正下方,是我期望触发的语句:

 if (window.QuestionSelector != 0 && window.QuestionSelector === -1) {
console.log("New question path");
$("#quizQuestion").unbind();

$("#quizQuestion").focusout(function () {
questionField();
console.log("Build Answers called");
buildAnswers();
$("input[name^=AnswerOrder]").unbind();
enableAnswerOrderSave();
$("[name^=AnswerID]").unbind();
autoCheckboxOnChange();
$("input[id^=IsValidAnswer]").unbind();
AnswerAcDeac();
$("input[name^=IsCorrectAnswer]").unbind();
AnswerCorrectUpdate();
})
}

使用 ===== 似乎没有什么区别。我已在调试器 window.QuestionSelector 中验证等于 -1。

最佳答案

问题是变量被设置为字符串,其计算结果与 int 非常不同。

在声明变量时,我必须使用 parseInt 函数修改代码。

window.QuestionSelector = parseInt($(this).attr('data-questionid'));

然后评估按预期进行。

关于javascript - jQuery 变量未按预期计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31279712/

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