[英]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页面,该页面将根据输入显示结果,而不刷新页面。
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”类的元素中。
$('form')
selects your form, you can personalize it $('form')
选择您的表单,您可以对其进行个性化设置 .submit
attach an event handler to the form "submit event .submit
将事件处理程序附加到“提交事件”表单 e.preventDefault()
and return false
prevents the form to be actually submitted e.preventDefault()
并return false
可防止实际提交表单 $('.content').load
will fill the element with "content" class with the ajax call results $('.content').load
将使用带有ajax调用结果的“content”类填充元素 this.action
is the URL to submit the form to this.action
是提交表单的URL " +.content"
is here to extract only the result part from the response, instead of the entire html page " +.content"
仅用于从响应中提取结果部分,而不是整个html页面 $(this).serialize()
passes the form fields data to the ajax request $(this).serialize()
将表单字段数据传递给ajax请求
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.