简体   繁体   English

原型如何用于onSubmit:function()?

[英]How does prototype work for onSubmit:function()?

I was working on Co-drops Minimal Form Interface. 我正在研究Co-drops最小表单界面。 I couldn't understand this code snippet in stepsForm.js. 我无法在stepsForm.js中理解此代码段。 (Line 50) (第50行)

stepsForm.prototype.options = {
        onSubmit : function() { return true; }
    };

I am new to JS, and wouldn't mind an explanation of the entire code in stepsForm if anyone has the time to do so. 我是JS的新手,如果有时间的话,不介意在stepsForm中解释整个代码。 But, for the time being, an explanation for the above can do wonders for me. 但是,就目前而言,对上述内容的解释可以为我带来奇迹。 I know what a prototype is, but the onSubmit part is going over my head. 我知道原型是什么,但是onSubmit部分让我头疼。 I read on another question that this is to prevent refresh, but I feel that is wrong. 我在另一个问题上读到,这是为了防止刷新,但是我觉得这是错误的。

The library exposes options property that you may/can use to pass your own overriding values.This one in particular, exposes onSubmit . 该库公开了options属性,您可以/可以使用它传递自己的覆盖值,尤其是公开onSubmit

For any html form an onSubmit is called when the submit action is invoked by another function or by click. 对于任何html form ,当其他函数或单击调用onSubmit操作时,都会调用onSubmit

In the library the default onSubmit is returning true, meaning just execute the action. 在库中,默认的onSubmit返回true,这意味着只需执行操作即可。 This can be overriden with you custom function like this... 可以使用您这样的自定义函数来覆盖...

<script>
    var FORM_ELEMENT = document.getElementById( 'myForm' )
    new stepsForm(FORM_ELEMENT, {
        onSubmit :
        function (FORM_ELEMENT) {
            alert('You are about to submit the form ');
            //manipulate your form or do any preprocess work... 

            return true;

        });
</script>   

Within the library the _submit (line 196 stepForm.js) is called which inturn calls the onSubmit . 在库中,将_submit (第196行stepForm.js),从而调用onSubmit This time, instead of the default, it will execute the one we added above. 这次,它将代替我们的默认值执行默认值,而不是默认值。

stepsForm.prototype._submit = function() {
    this.options.onSubmit(this.el);
}

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM