简体   繁体   English

待办事项列表在按Enter键后刷新

[英]To-do list refreshing upon hitting enter

Upon submitting a to-do list item, the page refreshes. 提交待办事项列表项后,页面将刷新。 I'm using e.stopPropagation() in an attempt to prevent the default page reload (doesn't help). 我正在使用e.stopPropagation()尝试阻止默认页面重新加载(无济于事)。

Here's my JavaScript. 这是我的JavaScript。 For my full code, see my CodePen . 有关我的完整代码,请参见CodePen

$(function() {

  $('.button').click(function(e) {
    e.stopPropagation();
    let toAdd = $('input[name=listItem]').val();
    // Inserts li as first child of ul
    $('ul').prepend('<li>' + toAdd + '</li>');
    // Clears input box after clicking '+'
    $('input[name=listItem]').val('');
  });

  // Upon hitting enter, triggers button click and prevents page from refreshing and event bubbling and capturing/trickling
  $('input[name=listItem]').keypress(function(e) {
    e.stopPropagation();
    if (e.which == 13) {
      $('.button').click();
    };
  });

  $('ul').on('click', 'li', function() {
    // Upon list item click, toggleClass() adds class 'strike' for it and fadeOut() completely hides it
    $(this).toggleClass('strike').fadeOut('slow');
  });

});

Use e.preventDefault() on the button to prevent the form submission. 在按钮上使用e.preventDefault()可以阻止表单提交。 You don't need e.stopPropagation in either handler. 您在任何一个处理程序中都不需要e.stopPropagation

When you want to disable the default keypress behaviour, you need to use preventDefault within the keypress event handler. 当您想要禁用默认的keypress行为时,您需要在keypress事件处理程序中使用preventDefault

Try changing to e.stopPropagation to e.preventDefault() : 尝试将e.stopPropagation更改为e.preventDefault()

$('input[name=listItem]').keypress(function(e) {
    e.preventDefault();
    if (e.which == 13) {
        $('.button').click();
    };
});

Change <button class="button">+</button> 更改<button class="button">+</button>

To <button type="button" class="button">+</button> In your HTML 要在您的HTML中<button type="button" class="button">+</button>

Since you haven't specified the type of button it is treated as a submit type button. 由于您尚未指定按钮的类型,因此将其视为提交类型按钮。 And hence on its click the page is being submitted. 因此,单击该页面即可提交。 So you just have to specify the button type as button . 因此,您只需将按钮类型指定为button即可

or just change e.stopPropagation(); 或只是更改e.stopPropagation(); to e.preventDefault(); e.preventDefault();

Hope this helps! 希望这可以帮助!

Add e.preventDefault() after the button click function 在按钮click功能后添加e.preventDefault()

if (e.which === 13) {
      $('.button').click();
      e.preventDefault()
    };

The problem is that the form is submitting when you trigger click on the button. 问题在于,触发按钮后click即提交表单。 Try adding this in your code: 尝试在您的代码中添加此代码:

$('form[name=listForm]').on('submit', function(e){
    e.preventDefault();
    return false;
});

I would suggest to remove the form, since you are never submitting it. 我建议删除该表单,因为您从不提交该表单。 Otherwise, I would suggest to just handle the form submission, and make you actions upon that event. 否则,我建议仅处理表单提交,并让您对该事件进行操作。 This way: 这条路:

$(function() {

  $('form[name=listForm]').on('submit', function(e) {
    e.preventDefault();
    let toAdd = $('input[name=listItem]').val();
    // Inserts li as first child of ul
    $('ul').prepend('<li>' + toAdd + '</li>');
    // Clears input box after clicking '+'
    $('input[name=listItem]').val('');
  });

  // Upon hitting enter, triggers button click and prevents page from refreshing and event bubbling and capturing/trickling
  $('input[name=listItem]').keypress(function(e) {
    if (e.which == 13) {
      $('form[name=listForm]').submit();
    };
  });

  $('ul').on('click', 'li', function() {
    // Upon list item click, toggleClass() adds class 'strike' for it and fadeOut() completely hides it
    $(this).toggleClass('strike').fadeOut('slow');
  });

});

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

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