jquery - 停止页面重新加载

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2099323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 12:46:03  来源:igfitidea点击:

jquery - stop page reload

jquery

提问by lamrin

I have Textboxand Button(Next and Previous) displayed in Lightbox. Currently my page is reloading upon clicking the Enter key. I dont want this to happen.

我有TextboxButton(下一个和上一个)显示在Lightbox. 目前我的页面在单击 Enter 键后重新加载。我不希望这种情况发生。

What i want is, when i click the Enter key, the "Next" button should be clicked without page reload which resides back of lightbox.

我想要的是,当我单击 Enter 键时,应单击“下一步”按钮,而无需重新加载位于灯箱后面的页面。

I used the following code, but both reloadand clickon Next button are happening:

我用下面的代码,但都reloadclick下一步按钮正在发生:

$("input").bind('keyup', function(event){
    if(event.keyCode == 13){
        $("#nextbtn").trigger('click');
    }
    event.preventDefault();
    return false;
});

回答by Gerard Banasig

Make sure your input button is type of button and not submit

确保您的输入按钮是按钮类型而不是提交

<input type='button'>

Then try this one if keycode is 13 then it will call the nextbtn click event, then truen false so it wont load back.

然后尝试这个,如果键码是 13 然后它会调用 nextbtn 点击事件,然后 truen false 所以它不会加载回来。

$("input").bind('keyup', function(event){ 
  if(event.keyCode == 13){ 
      $("#nextbtn").click(); 
      return false;
  }
});

回答by Rajeev Ranjan Lal

Put return false statement as soon as you trigger the Next button as follows:

触发 Next 按钮后立即放置 return false 语句,如下所示:

$("input").bind('keyup', function(event){ 
    if(event.keyCode == 13){ 
        $("#nextbtn").trigger('click');
        return false; 
    } 
    event.preventDefault(); 
});

回答by BBonifield

If I'm understanding your correctly, I think you just need to adjust your lightbox setup to use an iFrame. If you are just using a standard DOM element, the form submission is going to trigger a page load. The other way around that would be to use the jquery.form plugin to do AJAX form submissions.

如果我理解正确,我认为您只需要调整灯箱设置即可使用 iFrame。如果您只是使用标准 DOM 元素,则表单提交将触发页面加载。另一种方法是使用 jquery.form 插件来执行 AJAX 表单提交。

回答by Flash Thunder

The problem is somewhere else. As I understand the page is being reloaded because button is in the form and browser threads it as submit button. Please note, that this occures on clickevent, not on keyup. You can do two things:

问题出在其他地方。据我了解,页面正在重新加载,因为按钮在表单中,浏览器将其作为提交按钮进行线程处理。请注意,这发生在clickevent 上,而不是keyup. 你可以做两件事:

First. Assign clickevent to the button that returns false. Second. Change your input event to keypressinstead of keyupand add one more line: e.stopPropagation().

第一的。将click事件分配给返回 false 的按钮。第二。将您的输入事件更改为keypress而不是keyup并添加一行:e.stopPropagation()