简体   繁体   English

MVC 5,Ajax,Jquery-脚本只能运行一次

[英]MVC 5, Ajax, Jquery - script works only once

<script>
$(function () {

    var ajaxSubmit = function () {
        var $form = $(this);
        var settings = {
            data: $(this).serialize(),
            url: $(this).attr("action"),
            type: $(this).attr("method")
        };
        $.ajax(settings).done(function (result) {
            var $targetElement = $($form.data("ajax-target"));
            var $newContent = $(result);
            $($targetElement).replaceWith($newContent);
            $newContent.effect("slide");

        });
        return false;
    };

    $("#search-form").submit(ajaxSubmit);
});
</script>

Ok, This script is for searching some content from databse. 好的,此脚本用于从数据库搜索某些内容。 it works great, but only once. 它很棒,但只有一次。 When im trying to hit submit again in my from, its not working again. 当我试图从我再次点击提交时,它不再起作用。 Only when I refresh page. 仅在刷新页面时。 Could someone help me? 有人可以帮我吗?

My from in same index.cshtml file: 我来自同一index.cshtml文件:

<div>
<form id="search-form" method="get" data-ajax="true" data-ajax-target="#zawartosc" data-ajax-update="#zawartosc">
    <input id="search-filter" type="search" name="searchQuery"
           data-autocomplete-source="@Url.Action("MiejscaPodpowiedzi", "Miejsce")"
           placeholder="Wprowadź tekst, aby filtrować..." />
    <input type="submit" id="send" value="Send" />
</form>


<div id="zawartosc">
    @Html.Partial("_ListaMiejsc")
</div>

My controller: 我的控制器:

  public class HomeController : Controller
{
    private DaneKontekst db = new DaneKontekst();

    public ActionResult Index(string searchQuery = null)
    {
        var miejscaNaSwiecie = db.MiejscaNaSwiecie.Where(o => (searchQuery == null ||
        o.NazwaMiejscaNaSwiecie.ToLower().Contains(searchQuery.ToLower()) ||
        o.KrajMiejscaNaSwiecie.ToLower().Contains(searchQuery.ToLower()) ||
        o.OpisMiejscaNaSwiecie.ToLower().Contains(searchQuery.ToLower()))).ToList();

        var ViewModel = new HomeIndexViewModel()
        {
            MiejscaNaSwiecie = miejscaNaSwiecie
        };

        if (Request.IsAjaxRequest())
        {
            return PartialView("_ListaMiejsc", ViewModel);
        }

        return View(ViewModel);
    }

Edited. 编辑。

Because you are replacing the container. 因为您要更换容器。 You should update only the content of that. 您应该只更新其中的内容。

When you click for the first time, the response of the ajax call (the markup for the form) will replace the div (with id ="zawartosc"). 第一次单击时,ajax调用的响应(表单的标记)将替换div(标识为=“ zawartosc”)。 So after this you do not have that div exist in your DOM any more. 因此,在此之后,您的DOM中就不再存在该div。 So your $targetElement is not going be the the container div (because it is gone !) 因此,您的$targetElement不会成为容器div(因为它已经消失了!)

So instead of replacing the container div, simply update the content of that. 因此,无需替换容器div,只需更新其内容即可。

Replace 更换

 $($targetElement).replaceWith($newContent);

with

 $($targetElement).html($newContent);

$($targetElement).html($newContent); $($ targetElement).html($ newContent);

OR 要么

$($targetElement).Load($newContent); $($ targetElement).Load($ newContent);

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

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