JavaScript" if"声明不会运行

时间:2015-05-18 21:19:04

标签: javascript function if-statement

(使用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;

3 个答案:

答案 0 :(得分:1)

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

其次,您正在检查整数值。如果您只是阅读元素的<ListBox ItemsSource="{Binding}"> <ListBox.ItemTemplate> <DataTemplate DataType="DependencyObject"> <TextBlock Text="{Binding}" /> </DataTemplate> </ListBox.ItemTemplate> </ListBox> ,那么您将获得文本。

我猜,你想要的是:

value

但是假设var exercise = parseInt(txtExercise.value, 10); if(exercise === 0) { alert('Do you even lift?'); return; } 是一个输入元素。如果没有看到你的标记,就很难确定任何给定的答案是否有效。

答案 1 :(得分:1)

你走了,都修好了。你需要一个事件处理程序,这是一个更好的if / else用例。

http://codepen.io/davidwickman/pen/vOKjqV

// 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 + "!";
}

答案 2 :(得分:0)

假设您已正确关闭函数和if语句,则在if语句之前缺少一个半冒号。

 txtOutput1.value = "You destroyed, " + name + "!";  <---- here