简体   繁体   English

jQuery ajax调用仅在第一次有效

[英]jquery ajax call only works first time

I am updating list of users based on the user's search parameter. 我正在根据用户的搜索参数更新用户列表。 The ajax async request only works when page loads, no subsequent calls have an impact. ajax异步请求仅在页面加载时有效,后续调用均不受影响。 my search button seems to lose binding to the submit event. 我的搜索按钮似乎对提交事件失去了约束力。

Please point out how I can resolve this. 请指出我该如何解决。

Here is my js file code for the async request: 这是我的异步请求的js文件代码:

$(document).on("submit", "#test",null, function () {
    var $form = $(this); //wrap form element being submitted inside jquery to expose jquery functions. 

    var options = {

        url: $form.attr("action"),//url where this request should goto on the server.
        type: $form.attr("method"), //type of request - GET/POST
        data: $form.serialize() //suppress all inputs in the form element into name/value pairs as a string. 

    };

    //make async call to server with the above options.. Replace existing data on page with new data received from the async call to server.
    $.ajax(options).done(function (data) {
        var $target = $($form.attr("data-otf-target"));
        var $newHtml = $(data);
        $target.replaceWith($newHtml);
        $newHtml.effect("highlight");
    });

    return false; // don't request whole page.
});

Here is the view code: 这是视图代码:

<form method="get" id="test" action="@Url.Action("UserAccounts")" data-o-ajax="true" data-otf-target="#UserList">
    <div class="col-md-10">
        <input type="search" name="searchTerm" data-o-autocomplete="@Url.Action("AutoComplete", "Menu")"/>
        <input type="submit" value="Search"/>
    </div>

</form>

<div id="UserList">

    @Html.Partial("_Users", Model)
</div>

Finally fixed this issue that was driving me insane for 4 hours. 终于解决了这个使我发疯了四个小时的问题。 All it was a case of using .html instead of replaceWith function. 所有这些都是使用.html而不是replaceWith函数的情况。

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

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