亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

當我使用 jquery 附加到 HTML 表時,我無法觸發表行單擊事件

當我使用 jquery 附加到 HTML 表時,我無法觸發表行單擊事件

動漫人物 2023-09-11 15:15:45
我正在使用 asp.net mvc。我正在嘗試使用 jquery 附加到 HTML 表。我從 getJSON 調用獲取行并將它們附加到表中。行已附加,但由于某種原因,添加的行不會觸發單擊事件。我認為這可能與時間或其他原因有關,但我不確定。控制器using System;using System.Web;using System.Web.Mvc;namespace Test{    public class HomeController : Controller    {        public ActionResult Index()        {            return View();        }        public ActionResult Test(string x)        {            string strJson = "[{\"name\":\"tom\",\"number\":\"111\"},{\"name\":\"bill\",\"number\":\"222\"}]";            return Json(strJson, JsonRequestBehavior.AllowGet);        }    }}看法@{    Layout = null;}<style>    td {        border: 2px solid black;    }</style><br /><br /><table id="table1">    <tr>        <td>            name        </td>        <td>            number        </td>    </tr></table><br /><br /><button id="button1" type="button">append rows</button><script src="~/Scripts/jquery-3.4.1.js"></script><script>    //table click event    $(document).ready(function () {        $("#table1 tr").click(function () {            alert("table row clicked");        });    });    //button click event    $(document).ready(function () {        $("#button1").click(function () {            $.getJSON('@Url.Action("Test")', { x: "1" }, function (y) {                y = $.parseJSON(y);                $.each(y, function (i, item) {                    $('#table1').append('<tr><td>' + item.name + '</td><td>' + item.number + '</td></tr>');                });            });        });    });</script>
查看完整描述

1 回答

?
繁星淼淼

TA貢獻1775條經驗 獲得超11個贊

該事件未觸發,因為該行是在附加事件處理程序后添加的。使用:

$(document).on("click", "#table1 tr", function (){}


查看完整回答
反對 回復 2023-09-11
  • 1 回答
  • 0 關注
  • 100 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號