0
我寫了一個PHP腳本,檢查域是否可用於註冊或不。幫助我的第一個自己的對象在JavaScript/jQuery
要自動化的過程中,我也寫一個js腳本,自動發送AJAX調用到PHP腳本,並告訴用戶域名是否是可用的,不提交表單:
$(document).ready(function() {
function Domain() {
this.name = '';
this.dotComRegistered = 1;
this.dotNetRegistered = 1;
this.dotOrgRegistered = 1;
}
Domain.prototype.check = function(input) {
this.name = input;
if (this.name.length >= 3 && this.name.length <= 63) {
$.get('check.php', { d : this.name }, function(json) {
alert(json);
this.dotComRegistered = $.evalJSON(json).com;
this.dotNetRegistered = $.evalJSON(json).net;
this.dotOrgRegistered = $.evalJSON(json).org;
});
}
}
var domain = new Domain();
var input = ''
$('#domain').keyup(function() {
input = $('#domain').val();
domain.check(input);
});
$('form').submit(function() {
input = $('#domain').val();
domain.check(input);
return false;
});
});
由於你可以看到我創建了一個名爲Domain的對象,它代表了一個域名。該對象只有一個方法(除構造函數外)向PHP腳本(它返回json)發送AJAX請求。
問題是,Domain.prototype.check()方法不起作用(我沒有看到一個警告窗口),我不知道問題出在哪裏。當我將AJAX調用放在它工作的方法之外時,這不是問題。
我是一個OOP初學者,所以也許我使用了一些錯誤的語法來編寫Domain對象(我正在閱讀John Resig關於JavaScript中OOP的一本書)。
#domain是域名的輸入字段。
啊。我在劇本中將「dn」改寫爲「name」,並忘記在那裏重寫它。現在它工作:) – 2009-11-18 20:55:58
@理查德 - 很高興有幫助。附:你在閱讀哪本Resig書? – 2009-11-18 20:57:40
這一個:http://www.amazon.com/Pro-JavaScript-Techniques-John-Resig/dp/1590597273 – 2009-11-18 21:03:44