문제

I trying to invoke a public method of my jquery widget api from a form.submit but I'm having no success. Can anyone help me?

_cleanFormFields: function() {
  console.log("ok");
},

_addFormListener: function(map, marker) {
    var form = $(".add-form").clone().show();
    form.submit(function (event){
       event.preventDefault();
       _cleanFormFields();
    }
}           

Why this does not work?? The browser's console raises "Uncaught ReferenceError: _cleanFormFields is not defined" exception

도움이 되었습니까?

해결책

_cleanFormFields is a property of some object, right? So you can't call it directly, you need to reference it via your object:

yourObject._cleanFormFields();

Or, depending on how _addFormListener() is invoked you might be able to use this. But you'd need to keep a reference of the this from _addFormListener() because within the .submit() callback this will be the form element in question:

_addFormListener: function(map, marker) {
    var form = $(".add-form").clone().show(),
        self = this;
    form.submit(function (event){
       event.preventDefault();
       self._cleanFormFields();
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top