weixin_33724570 2015-04-22 14:15 采纳率: 0%
浏览 34

jQuery只运行一次

There is a simple solution with this issue. I have index page with partial view. partial view contains list of links to another views, "Back to List" link which gets list of links into partial view. Also index page has jQuery script with ajax which prevents page redirection and returns result into partial view. So, if I click any link first time, the result is displayed into partial view of Index page. Then I click "Back to List", and click any link second time the script is not working and page redirects. what is wrong?

Here is a code:

model "LinkList":

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace AjaxTest.Models
{
    public partial class LinkList
    {
        public int id { get; set; }
        public string label { get; set; }
        public string method { get; set; }
        public string controller { get; set; }        
        public string htmlLabel { get; set; }
    }
}

The controller has following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AjaxTest.Models;

namespace AjaxTest.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult PartialView(int id)
        {
            ViewBag.Id = id;
            return View();
        }

        public ActionResult PartialsList()
        {
            List<LinkList> list = new List<LinkList>() 
            {
                new LinkList{id = 1, label = "First partial", method = "PartialView", htmlLabel = "first-partial"},
                new LinkList{id = 2, label = "Second partial", method = "PartialView",htmlLabel = "second-partial"},
                new LinkList{id = 3, label = "Third partial", method = "PartialView", htmlLabel = "third-partial"}
            };

            return View(list);
        }
    }
}

And then 3 view pages PartialView.cshtml:

This is partial view with id = (@ViewBag.Id)

PartialsList.cshtml:

@model IEnumerable<AjaxTest.Models.LinkList>

<div>

    @foreach (var item in Model)
    {            
        @Html.ActionLink(item.label, item.method, item.controller, new { id = item.id }, new { id = item.htmlLabel })
        <br />
    }

</div>

And index.cshtml:

@{
    ViewBag.Title = "Index";
}

<script type="text/javascript">
    jQuery(document).ready(function () {
        $('#first-partial').click(function (event) {
            event.preventDefault();
            var url = $(this).attr('href');
            $('#pview').load(url);
        })
        $('#second-partial').click(function (event) {
            event.preventDefault();
            var url = $(this).attr('href');
            $('#pview').load(url);
        })
        $('#third-partial').click(function (event) {
            event.preventDefault();
            var url = $(this).attr('href');
            $('#pview').load(url);
        })
        $('#backToList').click(function (event) {
            event.preventDefault();
            var url = $(this).attr('href');
            $('#pview').load(url);
        })
    });
</script>

<h2>Index</h2>

<div id="index">
    <fieldset>
        <legend>Index</legend>
        This is Index page!
    </fieldset>
</div>
<div>
    <fieldset>
        <legend>Partials</legend>
        <br />
        <fieldset>            
            <div id="pview">
                @Html.Action("PartialsList", "Home", null)
            </div>
        </fieldset>
        <div>
            @Html.ActionLink("Back to list", "PartialsList", "Home", null, new { id = "backToList" })
    </div>
</fieldset>
</div>
  • 写回答

1条回答 默认 最新

  • 七度&光 2015-04-22 14:18
    关注

    If you want to bind events to dynamically created elements:

    $(document).on('click', 'yourSelector', function () {
        console.log('Event occured');
        // Your event handler function call here
    });
    

    jQuery DOC: https://api.jquery.com/on/

    https://stackoverflow.com/a/29728269/2025923

    https://stackoverflow.com/a/29788126/2025923

    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题