简体   繁体   English

访问嵌套对象文字对象未定义?

[英]Accessing nested object literal object undefined?

So I'm very new at object literal pattern but really excited about this pattern and the organization it provides. 因此,我对对象文字模式非常陌生,但对此模式及其提供的组织感到非常兴奋。

What I'm trying to do is update the phone number form field on-the-fly to format as users type (###-###-####). 我想做的是即时更新电话号码表单字段,以在用户键入时设置格式(###-###-####)。 However, I want to make the formatting functionality a sort of reusable utility so I can assign it accordingly to other inputs without rewriting the function's functionality every time...all the while trying to use the Object Literal pattern 但是,我想使格式化功能成为一种可重用的实用程序,因此我可以将其相应地分配给其他输入,而不必每次都重写函数的功能……在尝试使用对象文字模式时始终

I have a fiddle setup http://jsfiddle.net/JF3Vh/3/ 我有一个小提琴设置http://jsfiddle.net/JF3Vh/3/

Here's what I have thus far...Caution learning in progress... 到目前为止,这是我正在学习的警告。

HTML 的HTML

<div id="contactPage">
  <form>

    <label id="lblPhone" for="txtPhone">Your Phone Number:</label>
    <input id="txtPhone" name="phone" type="tel"/>                      

  </form>
</div>

JavaScript using jQuery 使用jQuery的JavaScript

var appMobile = {

    utilities: {

        formatPhone : function(){
            alert( "formatPhone is running" );
            $( this ).val().replace(/(\d{3})(\d{3})(\d{4})/, "$1-$2-$3");
        }

    },
    page: {
    home : {function()
       //Some Page specific functions here
    },
    contactForm: function () {
            $( '#txtPhone' ).onchange(function() {
                $(this).appMobile.utilities.formatPhone();
            });
        },
    products : {function()
       //Some Page specific functions here
    },


    }


};


$( document ).ready( function () {

    /*--initialize Scheduling page function--*/
    if ( $( '#contactPage' ) ) { appMobile.page.contactForm(); }

} );

Set me straight and thanks! 直说我,谢谢! :) :)

WizzyBoom WizzyBoom

I do believe this is what you were going for: 我确实相信这是您要的目的:

var appMobile = {
    utilities: {
        formatPhone: function(){
            alert( "formatPhone is running" );
            $( this ).val().replace(/(\d{3})(\d{3})(\d{4})/, "$1-$2-$3");
        }
    },
    page: {
        home: function() {
           //Some Page specific functions here
        },
        contactForm: function () {
            $('#txtPhone').on('change', appMobile.utilities.formatPhone);
        },
        products: function() {}
           //Some Page specific functions here
        }
    }
};

$(function () {
    if ($('#contactPage').length) { 
        appMobile.page.contactForm(); 
    }
});

Also, you may be interested in this masked edit plugin. 另外,您可能对该屏蔽的编辑插件感兴趣。

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

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