我正在使用 asp.net mvc。我正在嘗試使用 jquery 附加到 HTML 表。我從 getJSON 調(diào)用獲取行并將它們附加到表中。行已附加,但由于某種原因,添加的行不會觸發(fā)單擊事件。我認(rèn)為這可能與時間或其他原因有關(guān),但我不確定。控制器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貢獻(xiàn)1775條經(jīng)驗(yàn) 獲得超11個贊
該事件未觸發(fā),因?yàn)樵撔惺窃诟郊邮录幚沓绦蚝筇砑拥?。使用?/p>
$(document).on("click", "#table1 tr", function (){}
- 1 回答
- 0 關(guān)注
- 98 瀏覽
添加回答
舉報
0/150
提交
取消