简体   繁体   English

JQuery-AJAX表单提交但重新加载页面,更改URL

[英]JQuery-AJAX form submits but reloads page, changes url

I'm creating a Laravel application and am trying to get a form to submit without a page reload using AJAX. 我正在创建一个Laravel应用程序,并试图获取一个无需使用AJAX重新加载页面即可提交的表单。 I feel like I am getting really close, but I am just missing something. 我觉得我已经很接近了,但是我只是想念一些东西。

Here is my form: 这是我的表格:

<form name="new_stat" id="new_stat" role="form" >

My submit button: 我的提交按钮:

<input type="submit" id="addStatButton" />

Aaaand my Controller Method (ala Laravel): Aaa和我的控制器方法(ala Laravel):

public function store(Request $request) {
    $this->validate($request, [
      'stat' => 'required',
      'game_id' => 'required',
      'player_id' => 'required',
      'period' => 'required',
      'video_timestamp' => 'required',
    ]);

    $statToAdd = Stat_Meta::where('stat_abr', $request->input('stat'))->first()->id;

    $stat = new Stat;
    $stat->stat = $statToAdd;
    $stat->game_id = $request->input('game_id');
    $stat->player_id = $request->input('player_id');
    $stat->period = $request->input('period');
    $stat->video_timestamp = $request->input('video_timestamp');

    $stat->save();

    //I got rid of this thinking it would help stop the page reload. It didn't.
    //return redirect('/take-stats/1');
}

I tried this JS, but when I submitted the form, it registered the stuff to the database (yay!), but it reloaded the page and added parameters to the URL: 我尝试了这个JS,但是当我提交表单时,它把东西注册到了数据库中(是的!),但是它重新加载了页面并向URL添加了参数:

$('input#addStatButton').click( function() {
      $.post( '{{action("StatController@store")}}', $('form#new_stat').serialize(), function(data) {

         },
         'json' // I expect a JSON response
      );
  });

Now, after that, I read several places that I might wanna use 'submit' instead of 'click' if I didn't want the page reload, so I tried this: 现在,在那之后,如果我不想重新加载页面,我读了一些我想使用“提交”而不是“点击”的地方,所以我尝试了以下方法:

    $('form#new_stat').on('submit', function(e) {
    //e.preventDefault();
    $.post( 'post-stat', $('form#new_stat').serialize(), function(data) {

       },
       'json' // I expect a JSON response
    );
});

When I do this with 'e.preventDefault();' 当我使用'e.preventDefault();'进行此操作时 commented out, the page reloads, the url gets the parameters, but no record is added to the db. 注释掉,页面重新加载,URL获取参数,但没有记录添加到数据库。 When I don't comment it out, no url change, no page reload, no record added to the db. 当我不注释掉它时,没有url更改,没有页面重新加载,没有记录添加到数据库。

I am a TOTAL JQuery noob, I really prefer vanilla js, but I know that it is necessary to make this happen. 我是TOTAL JQuery新手,我真的更喜欢vanilla js,但我知道有必要做到这一点。 I am not sure if maybe Laravel is involved in making this not work, or what is happening, but that is in fact why I posted my question here lol 我不确定Laravel是否参与了这项工作,或者发生了什么事,但这实际上就是为什么我在这里发布问题的原因

You can change to type="button" to prevent reload page 您可以更改为type="button"以防止重新加载页面

<input type="submit" id="addStatButton" />

to

<input type="button" id="addStatButton" />

The submit type will cause a page reload when you submit. submit类型将导致页面重新加载。 You could change the type to button and use the click event itself. 您可以将类型更改为button并使用click事件本身。

Or other wise, use the click event on the submit type input and fire the e.preventDefault() at the start of the event-handler 否则, clicksubmit类型输入上使用click事件,并在事件处理程序的开始处触发e.preventDefault()

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

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