【问题标题】:jquery click event fires before clickjQuery点击事件在点击之前触发
【发布时间】:2016-09-19 01:57:10
【问题描述】:

我正在创建一个小型待办事项应用程序,遇到了一个问题,看起来警报消息在点击事件触发之前运行。看起来该事件根本没有注册。谁能告诉我为什么会这样?

(function() {
    'use strict';
    var MyTodo = {
        init: function() {
            this.cacheDom();
            this.bindEvent();
            this.addTask();

        },
        cacheDom: function() {
            this.$title = $('#inpTitle');
            this.$date = $('#inpDate');
            this.$description = $('#inpDescription');
            this.$addTaskBtn = $('#addTaskBtn');
            this.$pending = $('#pending');
        },
        addTask: function() {
            if (!this.$title.val()) {
                alert('please enter title');
                return;
            }
            var value = '<ul class="todo-task" id="todoList" draggable="true"><li><strong>' + this.$title.val() + '</strong></li>' +
                '<li>' + this.$date.val() + '</li>' +
                '<li>' + this.$description.val() + '</li></ul>';

            this.$pending.append(value);

            // empty inputs
            this.$title.val('');
            this.$date.val('');
            this.$description.val('');
        },
        bindEvent: function() {
            this.$addTaskBtn.on('click', this.addTask.bind(this));

        }

    };

    MyTodo.init();
})();

【问题讨论】:

    标签: javascript jquery angularjs node.js


    【解决方案1】:

    当您在init 中调用this.addTask(); 时,init 方法将被调用,因此它会alert()。从init() 中删除this.addTask 调用

    (function() {
      'use strict';
      var MyTodo = {
        init: function() {
          this.cacheDom();
          this.bindEvent();
    
        },
        cacheDom: function() {
          this.$title = $('#inpTitle');
          this.$date = $('#inpDate');
          this.$description = $('#inpDescription');
          this.$addTaskBtn = $('#addTaskBtn');
          this.$pending = $('#pending');
        },
        addTask: function() {
          if (!this.$title.val()) {
            alert('please enter title');
            return;
          }
          var value = '<ul class="todo-task" id="todoList" draggable="true"><li><strong>' + this.$title.val() + '</strong></li>' +
            '<li>' + this.$date.val() + '</li>' +
            '<li>' + this.$description.val() + '</li></ul>';
    
          this.$pending.append(value);
    
          // empty inputs
          this.$title.val('');
          this.$date.val('');
          this.$description.val('');
        },
        bindEvent: function() {
          this.$addTaskBtn.on('click', this.addTask.bind(this));
    
        }
    
      };
    
      MyTodo.init();
    })();
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <div id="todoWrapper" class="container">
      <div class="todo" id="pending" ondrop="drop(event)" ondragover="allowDrop(event)">
        <h3 class="text-center">Pending</h3>
    
    
      </div>
      <div class="todo" id="inProgress" ondrop="drop(event)" ondragover="allowDrop(event)">
        <h3 class="text-center">In Progress</h3>
      </div>
      <div class="todo" id="completed" ondrop="drop(event)" ondragover="allowDrop(event)">
        <h3 class="text-center">Completed</h3>
      </div>
      <div id="addTask" class="todo">
        <h2 class="text-center">Add Task</h2>
    
        <div>
          <input type="text" name="" id="inpTitle" placeholder="title">
        </div>
        <div>
          <textarea name="" id="inpDescription" cols="23" rows="3" placeholder="description"></textarea>
        </div>
        <div>
          <input type="date" name="" id="inpDate" placeholder="due date">
        </div>
        <div>
          <button id="addTaskBtn" class="btn btn-primary">Add Task</button>
          <button id="clearTaskBtn" class="btn btn-primary">Clear Task</button>
        </div>
    
    
    
        <div class="delete" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
        <audio id="sfx" src="media/dump.wav"></audio>
      </div>
    </div>

    【讨论】:

    • 我试过了,但没有帮助。它在页面加载时运行一次。
    • this.$addTaskBtn = $('#addTaskBtn'); 设置为 cacheDom 被首先调用。 OP 出于某种原因调用addTask,这就是触发警报的原因。它似乎应该只在点击事件时被调用
    • @PatrickEvans 我刚刚进行了更改,仍然在页面加载时触发
    猜你喜欢
    • 1970-01-01
    • 2011-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 2015-04-28
    • 1970-01-01
    相关资源
    最近更新 更多