jQuery函数调用问题

时间:2010-07-08 15:59:16

标签: jquery jquery-plugins

我有一个插件,我可以设置设置并在文档准备好时调用它。但是,我想将其更改为按钮的“onclick”而不是DOM ready。

插件就像

(function($) {

$.fn.addressSearch = function(settings) {

    settings = jQuery.extend({
        searchClass: "quickSearch",                             
        checkElement: "href",                                   
        dataElement: "data",                                   
        countryListClass: "countryList",                       
        countryCode: "11455",                                   
        errorMsg: "You can only search for address in the UK.",
        houseNumberClass: "TextboxHouseNumber",                 
        postcodeClass: "postcode",                              
        addressLine1Class: "addSearchLine1",                    
        addressLine2Class: "addSearchLine2",                    
        addressLine3Class: "addSearchLine3",                   
        addressTownCityClass: "addTownCity",                    
        ajaxUrl: "/WebService/addressLook",               
        submitType: "POST",                                     
        dataType: "xml",                                       
        parameters: "",                                         
        addressProcessURL: "",                                  
        callbackFunctionSingleAddress: selectAddress,           
        callbackFunctionMultipleAddress: quickBoxSearch,        
        useExternalProcessPage: false,                          
        validateCountry: true                                   

    }, settings);

    var jQueryMatchedObj = this;                                

    function _initialize() {
        _startModal(this, jQueryMatchedObj);                    
        return false;                                           
    }
    function _startModal(objClicked, jQueryMatchedObj) {
        $j(objClicked).addClass(settings.searchClass);

        var countryList = "." + settings.countryListClass + "";

        if (settings.validateCountry) {
            if ($j(countryList) && $j(countryList).val() != settings.countryCode) {
                alert(settings.errorMsg);
                return false;
            }
        }

        if (settings.parameters) {
            $j.ajax({
                url: settings.ajaxUrl,
                type: settings.submitType,
                dataType: settings.dataType,
                data: settings.parameters,
                success: function(res) {
                    var addresses = eval(res.getElementsByTagName('string')[0].firstChild.data);
                    if (addresses.length == 0)
                        alert('Your address could not be found, please enter it manually');
                    else if (addresses.length == 1) {
                        settings.callbackFunctionSingleAddress(
                            addresses[0].addressLine1,
                            addresses[0].addressLine2,
                            addresses[0].addressLine3,
                            addresses[0].town,
                            settings.TextboxHouseNumber,
                            settings.postcodeClass,
                            settings.addressTownCityClass,
                            settings.addressLine1Class,
                            settings.addressLine2Class,
                            settings.addressLine3Class
                            );
                    } else if (addresses.length > 1) {
                        settings.callbackFunctionMultipleAddress(
                            settings.callbackFunctionSingleAddress,
                            addresses,
                            settings.useExternalProcessPage,
                            settings.TextboxHouseNumber,
                            settings.postcodeClass,
                            settings.addressTownCityClass,
                            settings.addressLine1Class,
                            settings.addressLine2Class,
                            settings.addressLine3Class
                            );
                    }
                }
            });
        }
        return false;
    }
    //return this._initialize();
    return this.unbind('click').click(_initialize);
}
})(jQuery);

这可以解决在DOM上调用时的任何问题,我将其称为如下所示;

$("#mydiv").addressSearch({
   searchClass:"foo",
});

我想在同一个div的onclick上调用它。我该怎么做?当我在下面尝试时,它说“addressSearch undefined”

$("#mydiv").click(function(){

  addressSearch({
    searchClass:"foo",
  });

});

我哪里出错?

2 个答案:

答案 0 :(得分:2)

你已经编写了一个插件,所以你必须使用它就像jQuery的一部分。

$('#mydiv').click(function() {
  $(this).addressSearch(/* ... */);
});

答案 1 :(得分:0)

不需要:

$("#mydiv").click(function(){

  $("#mydiv").addressSearch({
    searchClass:"foo",
  });

});

或者甚至

$("#mydiv").click(function(){

  $(this).addressSearch({
    searchClass:"foo",
  });

});