3

最初のプロパティが、親オブジェクトがアクセスする子オブジェクトを決定する関数であるオブジェクトを作成したいと思います。私の特定のアプリケーションは、送信されるフォームの種類に応じて、使用されるフォーム検証の種類を変更しています。

ページの読み込み時に表示されるエラー メッセージは次のとおりです。Uncaught TypeError: Cannot set property 'validate' of undefined

これまでの私のJavascriptは次のとおりです(JSfiddleは次のとおりです: http://jsfiddle.net/WJRwv/3/ ):

var O={
  form:{
    voting:{
        validate: {}, success: {}
    },
    delete:{
        validate: {}, success: {}
    }
  }
};

O.form=function(formType){
  if(formType=='voting'){
    return O.form.voting;
  }
    else if(formType=='delete'){
    return O.form.delete;
  }
}

直下の行がエラーの原因です

O.form.voting.validate=function($form){ //**THIS LINE IS CAUSING THE ERROR**
  if($form.validate().form()){ // this is a method from the jQuery validate plugin
    console.log('YES validates');
  }
  else {
    console.log('NOT valid'); return false;
  }   
}

$('input[type=submit]').click(function(){
  var formType=$(this).data('form-type'),
      $form=$(this).closest('form'),
      formType=O.form(formType);
      console.log('form type is:'+formType);
      formType($form); //this should call the O.form.voting.validate method
}); 

HTML:

<form>
  <input type="submit" data-form-type='voting' name='voting' value="1">
</form>

<form>
  <input type="submit" data-form-type='delete' name='delete' value="1">
</form>
4

4 に答える 4

4

問題は、以前o.formに関数で上書きしたため、o.form.voting(最初に設定した他のものと共に) 存在しなくなったことです。

于 2012-10-09T15:29:50.503 に答える
2

オブジェクトリテラルO.formO.form関数によって上書きされています。その代わり...

var O = {
  form: function(formType) {
    if(formType=='voting'){
      return O.form.voting;
    }
      else if(formType=='delete'){
      return O.form.delete;
    }
  }
};

O.form.voting = {
  validate: {}, success: {}
};
O.form.delete = {
  validate: {}, success: {}
};
于 2012-10-09T15:32:12.647 に答える
1

これが問題を引き起こしています:

O.form=function

現在、プロパティformはありません。voting

于 2012-10-09T15:30:35.853 に答える
0

代わりにできることは、フォームをバリデーター設定にマップする関数を作成することです.jQueryバリデーターはフォームごとにバリデーターを許可し、フォームが送信されたときにどのバリデーターを使用するかを決定します...

更新された HTML...

<form data-form-type="voting">
  <input type="submit" name="voting" value="1"/>
</form>

<form data-form-type="delete">
  <input type="submit" name="delete" value="1"/>
</form>

更新された JS...

var o = {};
o.setFormValidations = function(types) {
    // Loop through forms and attempt to map each form to an item in the passed
    // types argument
    $("form").each(function() {
        // Get the form type from the data attribute
        var type = $(this).data("form-type");
        // Add in the validator if there is a matching item in the passed
        // types arguments
        if(types[type]) {
            $(this).validate(types[type]);
        }
    });
};

// On document ready we run the function passing in the jQuery validator
// settings for each form type
$(function() {
    namespace.setFormValidations({
        "voting" : {
            rules: {},
            messages: {},
            submitHandler: function() {
                // Do something for a valid form
            }
        },
        "delete" : {
            rules: {},
            messages: {},
            submitHandler: function() {
                // Do something for a valid form
            }
        }
    });
});
于 2012-10-09T16:06:03.580 に答える