简体   繁体   English

提交表单以触发Ajax请求

[英]Submitting a form to trigger an ajax request

I wish to have a "Contact us" form on my jquery-mobile web page. 我希望在我的jquery-mobile网站上有一个“联系我们”表格。 When the user has entered all the data correctly and pressed submit, an ajax post request should be made to an asp script. 当用户正确输入所有数据并按Submit后,应向asp脚本发出ajax发布请求。 If the asp script is successful, it will reply, via json, a success status to the javascript code that made the request. 如果asp脚本成功,它将通过json答复发出请求的javascript代码的成功状态。 I will then switch to another page within the very same html document and show "You message has been sent". 然后,我将切换到完全相同的html文档中的另一页,并显示“您的消息已发送”。

I have seen an example for doing this which uses no <form> or <input> tags. 我看过一个不使用<form><input>标记的示例。

Is it possible to do what I want with a <form> ? 是否可以用<form>做我想做的事情?

How would you do this in terms of html and what javascript events would you use? 就html而言,您将如何做?您将使用什么javascript事件?

Try this example : 试试这个例子:
HTML 的HTML

<form name="contactForm" id="contactForm">
    <input type="text" name="firstname" value="">
    <input type="submit" name="submit" id="submitBtn" value="Submit">
</form>  

JS JS

$(document).ready(function () {
    $("#submitBtn").click(function () {
        var formData = $("#contactForm").serialize();
        $.ajax({
            type: "POST",
            url: "YOUR FILE PATH", //serverside
            data: formData,
            beforeSend: function () {
                //show loading image
            },
            success: function (result) {
                console.log(result); //use this to see the response from serverside
            },
            error: function (e) {
                console.log(e); //use this to see an error in ajax request
            }
        });
    });
});

as what i have understood from the line in your question--"I will then switch to another page within the very same html document and show "You message has been sent". "-- In addition to Dh...'s answer if you want to go to another page on success add this before ajax call after you serialize the form data. 正如我从您的问题所在的行中所了解的-“然后,我将在相同的html文档中切换到另一页,并显示“您的消息已发送”。”-除了Dh ...的答复如果要成功转到另一个页面,请在序列化表单数据后在ajax调用之前添加此页面。

var goto=$('#yourFormId').attr('href')

and in then use goto in success callback. 然后在成功回调中使用goto

success: function()
{
  //at the end include this:
  location.href=goto;

}

NOTE: you can assign any address to goto and open that address at the time of success 注意:您可以将任何地址分配给goto并在成功时打开该地址

for JQM multi page structure you can trigger an click event on ajax success. 对于JQM多页面结构,您可以在ajax成功时触发click事件。 Suppose you have this in your html page. 假设您在html页面中有此标签。

<div data-role="page" id="page-one" data-title="Page 1">
    <div data-role="header">
        <h1>Page 1</h1>
    </div>
    <div data-role="content">
        <p>Body copy for page 1</p>
        <p><a href="#page-two" id="toPage2">Link to page 2</a></p>
    </div>
    <div data-role="footer">
        Copyright
    </div>
</div>
<div data-role="page" id="page-two" data-title="Page 2">
    <div data-role="header">
        <h1>Page 2</h1>
    </div>
    <div data-role="content">
        <p>Body copy for page 2</p>
        <a href="#page-one" id="toPage1">Link to page 1</a>
    </div>
    <div data-role="footer">
        Copyright
    </div>
</div>

And on success you want to open Page 2 whose id is page-two .Now the link of Page 2 is in the div Page 1 's <a> tag. 而成功要打开Page 2 id为page-two 。现在的链接Page 2是在div Page 1<a>标签。 So finally, instead of using location.href in ajax success callback you can use jquery .trigger() 因此,最后,可以使用jquery .trigger()代替在ajax成功回调中使用location.href

success: function()
{
  $('a#toPage2').trigger('click',function(){})

}
  1. include jquery into your html 将jquery包含到您的html中
  2. use $.ajax() to send form data to your asp and handle json accordingly. 使用$ .ajax()将表单数据发送到您的asp并相应地处理json。

Here is the document for using $.ajax: http://api.jquery.com/jQuery.ajax/ 这是使用$ .ajax的文档: http : //api.jquery.com/jQuery.ajax/

Some examples: http://www.w3schools.com/jquery/ajax_ajax.asp 一些示例: http : //www.w3schools.com/jquery/ajax_ajax.asp

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

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