JavaScript";如果;语句不会运行
(使用Javascript) 如果我在文本框中输入文本或不输入文本,警报将不会出现。 我知道这是一个简单的解决办法,但我不能想出它! (这是为了学习。) 训练日志测试JavaScript";如果;语句不会运行,javascript,function,if-statement,Javascript,Function,If Statement,(使用Javascript) 如果我在文本框中输入文本或不输入文本,警报将不会出现。 我知道这是一个简单的解决办法,但我不能想出它! (这是为了学习。) 训练日志测试 <script type="text/javascript"> function myResults() { myExercise(); myWeight(); mySets(); myReps(); myFunction(); } function myExercise()
<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;
函数myResults(){
myExercise();
我的体重();
mySets();
myReps();
myFunction();
}
函数myExercise(){
var txtExercise=document.getElementById(“txtExercise”);
var txtOutput=document.getElementById(“txtOutput1”);
var name=txtexe.value;
txtOutput1.value=“你销毁了,“+name+”!”
if(txtExercise.length==0){
警惕(‘你会举起吗?’);
返回;
假设已正确关闭函数和if语句,则在if语句之前缺少分号
txtOutput1.value = "You destroyed, " + name + "!"; <---- here
txtOutput1.value=“您销毁了,“+name+”!”;首先,您检查的是元素的“length”属性,而不是输入的值
其次,你要检查一个整数值。如果你只是简单地读取元素的值
,你会得到文本
我猜,你想要的是:
var exercise = parseInt(txtExercise.value, 10);
if(exercise === 0) {
alert('Do you even lift?');
return;
}
但这是假设txtexise
是一个输入元素。如果看不到您的标记,就很难确定任何给定的答案是否有效。好了,全部修复。您需要一个事件处理程序,这是一个更好的if/else用例
这就是所有的代码吗?因为它缺少了两个结尾},您也可以共享HTML吗?我想我们可以在这里使用更多的代码--您是否将myExercise()绑定到事件(例如,submit
或change
以获取输入)?是否要根据name.length进行测试?txtExercise.length将未定义,因为dom元素通常没有长度属性
// Check the .value.length instead of just the .length
if (txtExercise.value.length === 0) {
alert('Bro, do you even lift?');
} else {
txtOutput1.value = "You destroyed, " + name + "!";
}