簡體   English   中英

Ajax調用只有一次ASP.Net MVC5

[英]Ajax call goes only one time ASP.Net MVC5

我在ASP.Net MVC5中編寫一個AJAX函數,並且遇到一個問題,即表單AJAX請求僅執行一次。 這是一個搜索頁面。 選擇過濾器后,按搜索,我得到正確的結果。 但是,如果我更改過濾器並再次單擊搜索提交,則不會發生任何事情。

var ajaxFormSubmit = function() {
    var $form = $(this);
    var options = {
        url: $form.attr("action"),
        type: $form.attr("method"),
        data: $form.serialize()
    };

    $.ajax(options).done(function (data) {
        var target = $($form.attr("data-enbw-target"));
        target.replaceWith(data);
        debugger;
    });

    return false;
};
$("form[data-enbw-ajax='true']").submit(ajaxFormSubmit);
<form method="get" id="documentForm" action="@Url.Action("Index", "DocumentSearch")" def data-enbw-ajax="true" data-enbw-target="#documentSearchResult">
    <button type="submit" id="submitbtn" name="submitbtn" tabindex="100" class="k-button">
        <img src="~/Content/search_small_icon.png" /> 
        @WebResources.DocumentSearchButton
    </button>
</form>
@Html.Partial("Results", @Model)
public ActionResult Index(DocumentSearchInput model)
{
    if (Request.IsAjaxRequest())
    {
        return PartialView("Results", result);
    }

    return View(result);
}

我沒有任何錯誤。 當我得到一個調試器時; 在javascript中。 新數據正確。 你能幫我么。

您將在ajax成功中替換表格。 因此,新表單將沒有提交綁定。 如果您確實要執行此操作,則必須重新綁定到新表單,或者可能使用委托。

$('parentSelector').on('event', 'childSelector', function(){});

parentSelector-子元素的父元素,該子元素預先存在該子元素,通常不應在頁面生命周期中刪除/創建。

childSelector-元素的選擇器,將在頁面壽命中的某個時間點創建/更改/刪除該元素。

我找到了答案。 問題不在於提交。 問題出在重寫數據上。

$.ajax(options).done(function (data) {
    $("#documentSearchResult").empty();
    $("#documentSearchResult").html(data);
});

簡單來說,我清空div,然后在里面寫。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM