【问题标题】:how do i attach click handler/any event handler to my object or module我如何将点击处理程序/任何事件处理程序附加到我的对象或模块
【发布时间】:2018-07-29 13:10:41
【问题描述】:

我想将事件处理程序附加到我不知道如何实现的模块。

下面是我的模块:

var globalModule = {

  name:'Banana on princess bed',

     init:function(){
         alert('Initialize');
     },

     getName:function(){
        return this.name; 
     }
}

上述module 我将为我的应用程序的每个页面加载,或者说在index 页面中加载(在所有其他页面中可用)

假设这是page 1(除了索引页面)我想听按钮点击

<button id="button1">button 1</button>

$('#button1').on('click',function(){
    // execute some code
});

问题:我如何处理来自我的globalModulebutton1 click 并处理它

var globalModule = {

  name:'Banana on princess bed',
    
     init:function(){
         alert('Initialize');
     },

     getName:function(){
        return this.name; 
     }
}

globalModule.init();

var name = globalModule.getName();

console.log(name);

【问题讨论】:

    标签: javascript jquery node.js


    【解决方案1】:

    那么,为什么您认为在 globalModule 中声明点击事件还不够好? 请告诉我详情。

    var globalModule = {
    
      name:'Banana on princess bed',
        
         init:function(){
             alert('Initialize');
             // Attach an event.
             $('.common1').on('click', function() {
                 console.log('target-modal : ' + $(this).data('targetModal'));
             });
             $('.modal-submit').on('click', function() {
                 // you need to manage to work properly.
                 // Get root, then find parameter.
                 // Of course, If you use form and prevent submit,
                 // jquery serializeArray() is a good option.
                 var thisModal = $(this).closest('.modal-root');
                 console.log(thisModal.find('.param1').val());
                 console.log(thisModal.find('.param2').val());
                 console.log('go ajax : ' + $(this).data('url'));
             });
         },
         getName:function(){
            return this.name; 
         }
    }
    
    globalModule.init();
    
    var name = globalModule.getName();
    
    console.log(name);
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    ■other page1
    <input class="common1" id="page1-common1" type="button" value="Common Button?" data-target-modal="modal1" data-url="[1]if ajax url is differenc, you need it">
    <div id="modal1" class='modal-root'>
        <input type="text" value="page1-param1" class="param1">
        <input type="text" value="page1-param2" class="param2">
        <input class="modal-submit" type="button" value="submit1" data-url="[1]if ajax url is differenct, you need it">
    </div><br>
    ■other page2
    <input class="common1" id="page2-common1" type="button" value="Common Button?" data-target-modal="modal2">
    <div id="modal2" class='modal-root'>
        <input type="text" value="page2-param1" class="param1">
        <input type="text" value="page2-param2" class="param2">
        <input class="modal-submit" type="button" value="submit2" data-url="[2]if ajax url is differenc, you need it">
    </div>

    【讨论】:

    • 在每个页面上我必须init 每个特定于页面的click 处理程序我不能这样做吗?如果可以的话,请帮助我
    • click 的每个页面 中的一些button 我必须显示modalpost 的数据
    • 我假设你在每一页都包含这个模块。通常,如果您想创建一个公共事件,您只需在该文件中创建一个common file(like common.js),然后是attach an event。这样,您就不需要该模块。如果每个页面的流程不同,我认为包含 globalModule 没有意义,但就您而言,我认为您可以这样做。
    • 你说的“post”,是用ajax,还是普通提交?
    • ajax 无处不在
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多