你好,我正在搞亂Javascript中的對象。這是我的對象在一個單獨的.js文件中。JavaScript對象只提醒第一個選項
JSON-webservice.js
//create object
function objdata(tool, product, details) {
//create object properties
this.tool = tool;
this.product = product;
this.details = details;
//create object methods
this.validate = function() {
var error = 0;
for (var prop in this) {
if (this.hasOwnProperty(prop)) {
if (prop != 'validate' || prop != 'submit') {
if (this[prop] == null || this[prop] == undefined || this[prop] == "") {
error += 1;
}
}
}
}
return error;
}
this.submit = function() {
var error = this.validate();
if (error > 0) {
alert("errors: " + error);
}
else {
alert(this.tool + " " + this.product + " " + this.details);
}
}
}
}
我包括我的index-main.html
頁面的頭部這個腳本。
然後準備好文件,然後初始化新的對象並提交。
$(document).ready(function() {
var userdata = new objdata('5', 'Main Page', '9');
userdata.submit();
});
我的問題是爲什麼它只警報
---------------------------
Windows Internet Explorer
---------------------------
5
---------------------------
OK
---------------------------
,而不是
---------------------------
Windows Internet Explorer
---------------------------
5 Main Page 9
---------------------------
OK
---------------------------
喜歡我認爲應該。我的對象是否正確編碼?我沒有在IE中報告任何javascript錯誤。
的奇怪的是我可以複製JSON-webservice.js文件內容(對象上)和它完美。儘管如此,它不能用作外部文件。
考慮使用'控制檯。 log()'(和一個真正的瀏覽器),而不是alert()來進行調試。 –
你可以減少'if'語句到'if(!this.prop)' – elclanrs
哦,我會喜歡!這個工作太糟糕了,我擁有的是IE7¬o(¯-メ) – atrueresistance