简体   繁体   English

如何让AJAX函数处理提交表单

[英]How to let the AJAX function handle the submit form

I am creating this simple search form 我正在创建此简单的搜索表单

<form id="myForm" method="POST">
    <table>
        <tr>
            <td><input type="text" name="search" placeholder="Search Accounts" /></td>
            <td><button type="submit">Search</button></td>
        </tr>       
    </table>
    <br/>
</form>

I don't want my form to go to another page. 我不希望我的表格转到另一页。 That is why there is no action. 这就是为什么不采取任何行动的原因。 I would like AJAX function to handle that. 我想要AJAX函数来处理。 But it is not working. 但这是行不通的。

I have tried doing the javascript, 我已经尝试过使用javascript,

$('#myForm').click(function(){
    $.ajax({
            url: ROOT_URL + 'account/send',
            type: "post",
            dataType: "text"
        }).done(function(data) {
            $('#accounts-container').html(data);
    });
});

When I try to implement this, It directed me to the home page. 当我尝试实现此功能时,它将我定向到主页。

EDIT 编辑

Update: The .submit and preventDefault works. 更新:.submit和preventDefault有效。 The problem now is the data. 现在的问题是数据。 Where does it go? 去哪儿了? I need the data(name="search") textbox back to my controller. 我需要data(name =“ search”)文本框回到我的控制器。

In my controller I collect the data, 在我的控制器中,我收集数据,

List<Users> list = accountService.getUserList(search);

If it were just a form with action, the form will be submitted along with the data. 如果它只是一个带有动作的表单,则该表单将与数据一起提交。

Please read this before used AJAX function handle the submit form 请在使用AJAX功能处理提交表格之前阅读此书

  $('#myForm').submit(function(){ $.ajax({ url: ROOT_URL + 'account/send', type: "post", dataType: "text" }).done(function(data) { $('#accounts-container').html(data); }); }); 

Please try this. 请尝试这个。

Ajax Function used submit event. 使用了Ajax Function的Submit事件。

You need to change the $('#myForm').click() to $('#myForm').submit() 您需要将$('#myForm').click()更改$('#myForm').submit()

Than you have to cancel the submit event. 比您必须取消提交事件。 Explanation from jQuery API Dock : jQuery API Dock的说明:

Now when the form is submitted, the message is alerted. 现在,提交表单后,将提示该消息。 This happens prior to the actual submission, so we can cancel the submit action by calling .preventDefault() on the event object or by returning false from our handler. 这发生在实际提交之前,因此我们可以通过在事件对象上调用.preventDefault()或从处理程序中返回false来取消提交操作。

$( "#target" ).submit(function( event ) {
     $.ajax({
        url: ROOT_URL + 'account/send',
        type: "post",
        dataType: "JSON",
        data: { name: "search" },
     }).done(function(data) {
        $('#accounts-container').html(data);
     });
     event.preventDefault();
});

In this case the submit event executes the ajax call, then it prevents the default behavior of the submit event (it doesn't reload the page). 在这种情况下,submit事件将执行ajax调用,然后它会阻止Submit事件的默认行为(它不会重新加载页面)。

There are 3 ways to prevent a change of your page with little changes in your code: 只需很少更改代码,就可以通过3种方法来防止页面更改:

1) Change your button into 1)将按钮更改为

<button type="button">Submit</button>

To prevent it form submitting your form, and just triggering you AJAX function which handles the submission; 为了防止表单提交您的表单,而只是触发您处理提交的AJAX函数;

2) Change the "trigger" of your jquery function to 2)将您的jquery函数的“触发器”更改为

$('#myForm').submit(function(){

so it will fire jquery on submit and as default behavior will prevent action redirect from happening; 因此它将在提交时触发jquery,并且默认行为将阻止操作重定向发生;

3) add a 3)添加一个

event.preventDefault();

or 要么

return false;

immediately before closing jour AJAX function. 关闭jour AJAX功能之前。

For what concerns collecting yor data, you just have to pass the value of your input to your AJAX function (assign the id="searchinput" to your search text input): 对于收集数据的问题,您只需将输入值传递给AJAX函数(将id =“ searchinput”分配给搜索文本输入):

url: ROOT_URL + 'account/send',
data: {yourvarname:$("#searchinput").val()},
type: "post",

<input type="text" name="search" id="searchinput" placeholder="Search Accounts" />

You said I dont want my form to go to another page. That is why there is no action. 您说I dont want my form to go to another page. That is why there is no action. I dont want my form to go to another page. That is why there is no action.

If so add onclick="return false" to your element and try it! 如果是这样,请在您的元素中添加onclick =“ return false”并尝试!

 //add onclick false
 <form id="myForm" method="POST" onclick="return false;">
        <table>
            <tr>
                <td><input type="text" name="search" placeholder="Search Accounts" /></td>
                <td><button type="submit">Search</button></td>
            </tr>       
        </table>
        <br/>
    </form>

or add return false; 或添加return false; to your ajax function last line 到你的ajax函数的最后一行

$('#myForm').click(function(){
    $.ajax({
            url: ROOT_URL + 'account/send',
            type: "post",
            dataType: "text"
        }).done(function(data) {
            $('#accounts-container').html(data);
    });
    return false;//here 
});

 $('#btnSubmit').click(function(){ alert("Here you can make your ajax request and get response from action"); var value= "search text";// please get your search value here and pass. $.ajax({ url: ROOT_URL + 'account/send', data: {search: value}, type: "post", dataType: "text" }).done(function(data) { $('#accounts-container').html(data); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script> <button type="submit" id="btnSubmit">Search</button> 

Use button type instead of submit and add id attribute. 使用按钮类型代替提交和添加id属性。 So change 所以改变

<td><button type="submit">Search</button></td>
to
<td><button type="button" id="submitFormBtn">Search</button></td>

Add id to your textbox. 将ID添加到您的文本框。

<input type="text" id="search" name="search" placeholder="Search Accounts" />

In you JS, use click function on submitFormBtn. 在您的JS中,对SubmitFormBtn使用click函数。

$('#submitFormBtn').click(function(){
$.ajax({
        url: ROOT_URL + 'account/send',
        type: "post",
        data : {searchText : $('#search').val()},//get the textbox value and pass it
        dataType: "text"
    }).done(function(data) {
        $('#accounts-container').html(data);
  });
});

On your server side method, use the variable name - searchText to get the data 在服务器端方法上,使用变量名称-searchText来获取数据

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

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