gpt4 book ai didi

JavaScript "if"语句无法运行

转载 作者:行者123 更新时间:2023-12-02 16:10:03 25 4
gpt4 key购买 nike

(使用 JavaScript)如果我在文本框中输入文本或不输入文本,则不会出现警报。我知道这是一个简单的修复,但我无法弄清楚!(这是为了学习目的。)

锻炼日志测试

<script type="text/javascript">

function myResults() {
myExercise();
myWeight();
mySets();
myReps();
myFunction();
}

function myExercise() {
var txtExercise = document.getElementById("txtExercise");
var txtOutput = document.getElementById("txtOutput1");
var name = txtExercise.value;
txtOutput1.value = "You destroyed, " + name + "!"

if (txtExercise.length === 0) {
alert ('Do you even lift?');
return;

最佳答案

首先,您要检查元素的“length”属性,而不是输入的值。

其次,您要检查整数值。如果您只是读取元素的,您将获得文本。

我猜,你想要的是这样的:

var exercise = parseInt(txtExercise.value, 10);
if(exercise === 0) {
alert('Do you even lift?');
return;
}

但这是假设 txtExercise 是一个输入元素。如果没有看到您的标记,就很难确定任何给定的答案是否有效。

关于JavaScript "if"语句无法运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30312965/

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