开发者

Help with my first own object in JavaScript/jQuery

I have written a PHP script that checks whether a domain is available for registration or not.

To automate the process, I have also written a js script that automatically sends AJAX calls to the PHP script and tells the user whether the domain is available or not, without submitting the form:

$(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.dotN开发者_开发问答etRegistered = $.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;

    });

});

As you can see I created an object called Domain which represents a domain name. The object has just one method (besides the constructor) that sends AJAX request to the PHP script (which returns json).

The problem is that the Domain.prototype.check() method doesn't work (I don't get an alert window) and I don't know where's the problem. When I place the AJAX call outside of the method it works, so that isn't the problem.

I'm an OOP beginner so maybe I used some wrong syntax to write the Domain object (I'm reading a book from John Resig about OOP in JavaScript right now).

The #domain is the input field for domain names.


have you stepped through with Firebug? Set a breakpoint at the point the request is made and step through from that point. Where does the dn come from in your code?

Also, is there a specific reason that your Domain function is in $(document).ready()? It doesn't really need to be there (you might consider namespacing your classes too).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜