简体   繁体   English

如何使用jquery-ajax提交表单数据?

[英]How do i submit form data using jquery-ajax?

I'm not sure why I'm not getting it. 我不知道为什么我没有得到它。 Either way this is frustrating me beyond belief. 无论哪种方式,这让我感到沮丧。 I've looked through 2 jquery books, web tutorials, and stack overflow posts and i still dont understand. 我查看了2个jquery书籍,网络教程和堆栈溢出帖子,我仍然不明白。

I am trying to build a search filter bar where a user can select filters and upon clicking "refine result" it modifies the query output. 我正在尝试构建一个搜索过滤器栏,用户可以在其中选择过滤器,点击“精炼结果”后,它会修改查询输出。

How do I submit the value of whichever form radio box the user selects using jquery/ajax to a php page which will display results dependent upon the input, without page refresh. 如何使用jquery / ajax将用户选择的任何形式单选框的值提交到php页面,该页面将根据输入显示结果,而不刷新页面。

  1. Form--> 表 - >
  2. user selects radio button value and clicks submit--> 用户选择单选按钮值并单击提交 - >
  3. form data gets sent via jquery/ajax to the .php page whose output is to be displayed in without page refresh--> 表单数据通过jquery / ajax发送到.php页面,其输出将在没有页面刷新的情况下显示 - >
  4. .php file processes the user input and creates output based on it .php文件处理用户输入并基于它创建输出

I've honestly looked through alot of different answers but all of them assume that the reader has a greater basic knowledge of javascript/jquery than I do. 我老实地看了很多不同的答案,但所有人都认为读者比我更了解javascript / jquery的基本知识。

Like, do I have to put the jQuery/ajax in a function that runs "onclick" of my submit button? 比如,我是否必须将jQuery / ajax放在运行“onclick”提交按钮的函数中? Or do I just add a .click event handler for my button id? 或者我只为我的按钮ID添加一个.click事件处理程序?

Can someone explain it all to me assuming I'm a complete jQuery/ajax noob? 假设我是一个完整的jQuery / ajax noob,有人可以解释一下吗?

You just have to attach your ajax call to the "submit" event that comes from the form when the submit button is clicked : 您只需将ajax调用附加到单击提交按钮时来自表单的“submit”事件:

$('form').submit(function(e){
    e.preventDefault();
    $('.content').load(this.action+" .content", $(this).serialize(), function(){
        // code to execute after result display if needed
    });
    return false;
});

I assumed your results are displayed in an element that has the "content" class. 我假设您的结果显示在具有“content”类的元素中。

  1. $('form') selects your form, you can personalize it $('form')选择您的表单,您可以对其进行个性化设置
  2. .submit attach an event handler to the form "submit event .submit将事件处理程序附加到“提交事件”表单
  3. e.preventDefault() and return false prevents the form to be actually submitted e.preventDefault()return false可防止实际提交表单
  4. $('.content').load will fill the element with "content" class with the ajax call results $('.content').load将使用带有ajax调用结果的“content”类填充元素
  5. this.action is the URL to submit the form to this.action是提交表单的URL
  6. " +.content" is here to extract only the result part from the response, instead of the entire html page " +.content"仅用于从响应中提取结果部分,而不是整个html页面
  7. $(this).serialize() passes the form fields data to the ajax request $(this).serialize()将表单字段数据传递给ajax请求
  8. finally the last anonymous function is called after the results are displayed and can be used to trigger some visual effect indicating that the results were updated 最后,在显示结果后调用最后一个匿名函数,并可用于触发一些表示结果已更新的视觉效果

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

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