gpt4 book ai didi

JavaScript 确认框返回代码而不是函数值

转载 作者:行者123 更新时间:2023-11-30 12:48:29 26 4
gpt4 key购买 nike

我创建了一个真正简单的表单并使用 JS 访问单选按钮的值,然后进行快速计算并基于函数在弹出框中返回一个字符串。问题是当单击按钮时,弹出窗口返回被调用函数的代码而不是字符串。

     <div id="questionaire">
<div id="question1">
<h2>How familiar are you with Javascript?</h2><br>
<form id="first_q">
<input type="radio" name="q1" value="4">Very Experienced.</input><br>
<input type="radio" name="q1" value="3">I've Worked with the Basics.</input><br>
<input type="radio" name="q1" value="2">I Only Know Its a Programming Language.</input><br>
<input type="radio" name="q1" value="1">Java...Mmm, Coffee sounds GOOD!</input>
</form>
</div>
<div id="question2">
<h2>How much do you like to code?</h2><br>
<form id="second_q">
<input type="radio" name="q2" value="4">I think about coding during sex!</input><br>
<input type="radio" name="q2" value="3">It's safe to say, I take coding pretty_seriously.</input><br>
<input type="radio" name="q2" value="2">I would consider it my cold-weather hobby.</input><br>
<input type="radio" name="q2" value="1">Technology is cool.</input>
</form>
</div>
</div>
<div id="answer">
<button id="color-switcher">TELL ME MORE...</button>
</div>

外链JS

var q1_rating = function() {
var first_q_answer = document.getElementsByName("q1");

for(var i = 0; i < first_q_answer.length; i++) {
if(first_q_answer[i].checked == true) {
q1_value = first_q_answer[i].value;
}
}

return parseFloat(q1_value);
};

var q2_rating = function() {
var second_q_answer = document.getElementsByName("q2");

for(var i = 0; i < second_q_answer.length; i++) {
if(second_q_answer[i].checked == true) {
q2_value = second_q_answer[i].value;
}
}

return parseFloat(q2_value);
};

var final_rating = function() {
total_value = (q1_rating + q2_rating);

if (total_value > 6) {
return "You're a TRUE NERD ready to conquer the world.";
} else if (total_value > 4 && total_value <=6) {
return "You need some more work, but you're coming along nicely.";
} else if (total_value >2 && total_value <=4) {
return "You need to step up your nerd game pronto!";
} else {
return "You're more jock than nerd";
}
};

document.getElementById('color-switcher').addEventListener('click', function(){
confirm(final_rating);
})

最佳答案

你需要调用final_rating函数

 confirm(final_rating());

目前,您正在传递函数的引用

关于JavaScript 确认框返回代码而不是函数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21787280/

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