var fiat = {
make: fiat,
model: "500",
year: 1957,
color: "medium Blue",
passengers: 2,
convertible: false,
mileage: 88000,
started: false,
start: function() {
started = true;
},
stop: function() {
started = false;
},
drive: function() {
if (started) {
alert("Zoom Zoom!");
} else {
alert("Car needs to be started first.");
}
}
};
fiat.start();
fiat.drive();
所以它一直说未定义变量。这就是它向我展示如何在书中写出来的。我也试着说,if(fiat.started),但那也没有用。还有其他方法可以写吗?
答案 0 :(得分:1)
<!DOCTYPE HTML>
<html>
<script src="//code.jquery.com/jquery-1.9.1.js"></script>
<script src="//ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js"></script>
<script>
$( document ).ready(function() {
$("#ip1").change(function() {
$('#simpleform').valid()});
})
$("#ip2").change(function() {
$('#simpleform').valid()});
})
$("#simpleform").validate({ //use this during ip1 change
rules: {
frequency: "required"
},
messages: {
frequency: "Please specify your frequency"
}
});
$("#simpleform").validate({ //use this during ip2 change
rules: {
adsize: "required"
},
messages: {
adsize: "Please specify adsize"
}
});
</script>
<body>
<form id="simpleform">
Frequency : <input type="text" name="frequency"><br>
Ad Size: <input type="text" name="adsize"><br>
Input 1:
<select id="ip1">
<option>A</option>
<option>B</option>
<option>C</option>
</select>
<br>
Input 2:
<select id="ip2">
<option>X</option>
<option>Y</option>
<option>Z</option>
</select>
<br>
<input type="submit">
</form>
</body>
</html>
未定义。这相当于写started
。使用foo = "bar"
或将其附加到对象中的预定义var
来定义它。
如果您想将其设为全局变量,请将started
添加到代码的开头。
在您的情况下,通过对象属性调用该函数,将var started;
内的started
和start()
替换为stop()
。