C# Ajax请求不适用于动态控件追加

C# Ajax请求不适用于动态控件追加,c#,jquery,ajax,asp.net-mvc-2,C#,Jquery,Ajax,Asp.net Mvc 2,我已经参考并尝试在link的click事件上动态插入控件。就我而言,它不起作用。我不知道它怎么了。删除Div(包括控件)效果良好。但是附加控件不起作用。当我点击“添加更多”链接时,它会在新页面上打开。不呈现在同一页上添加控件 我的主视图代码: <div id="Div1"> <% Html.RenderAction("_EditServices", "CRM", new { id = Model.Id });%> </div> <div id="ed

我已经参考并尝试在link的click事件上动态插入控件。就我而言,它不起作用。我不知道它怎么了。删除Div(包括控件)效果良好。但是附加控件不起作用。当我点击“添加更多”链接时,它会在新页面上打开。不呈现在同一页上添加控件

我的主视图代码:

<div id="Div1">
  <% Html.RenderAction("_EditServices", "CRM", new { id = Model.Id });%>
</div>
<div id="editorRows">
  <% Html.RenderAction("_EditInsertServices", "CRM"); %>
</div>
<%= Html.ActionLink("+ Add More Service(s)", "EditAdd", null , new { id = "addItem" })%>
脚本:

<script type="text/javascript">
    $("#addItem").click(function () {
        $.ajax({
            url: this.href,
            cache: false,
            success: function (html) { $("#editorRows").append(html); }
        });
        return false;
    });
    $("a.deleteInsertRow").live("click", function () {
        $(this).parents("div.editorRow:first").remove();
        return false;
    });
</script>

$(“#添加项”)。单击(函数(){
$.ajax({
url:this.href,
cache:false,
成功:函数(html){$(“#editorRows”).append(html);}
});
返回false;
});
$(“a.deleteInsertRow”).live(“单击”,函数(){
$(this).parents(“div.editorRow:first”).remove();
返回false;
});
live()已弃用,并在jQuery 1.9中删除

  $('#editorRows').on("click", "a.deleteInsertRow",function () {
    $(this).parents("div.editorRow:first").remove();
    return false;
});

如果你想了解更多关于事件的信息,我已经解决了。实际上我放了$(“#addItem”).click(function(){});函数转换为$(document).ready(函数(){});功能正常,工作正常。 因此,每当我使用$(“any class/id”)时,我都应该将该方法放在$(document.ready()函数中

以下是解决方案:

<script type="text/javascript">
$(document).ready(function () {
  $("#addItem").click(function () {
    $.ajax({
    url: this.href,
    cache: false,
    success: function (html) { $("#editorRows").append(html); }});
    return false;
  });
});
</script>

$(文档).ready(函数(){
$(“#添加项”)。单击(函数(){
$.ajax({
url:this.href,
cache:false,
成功:函数(html){$(“#editorRows”).append(html);};
返回false;
});
});

My adding控件不起作用。删除Div工作正常。您使用的jQuery版本是什么?您是否在控制台中进行了检查1)您没有错误2)请求的页面是否正确接收?@dystroy,我没有收到任何错误,除了“ReferenceError:$未定义”之外,我单击“添加更多链接”。我认为Html.ActionLink中可能有问题。我的jquery版本是1.9,所以很明显你不能使用
live
@dystroy,我试过了,但不起作用。但生活就是工作。
  $('#editorRows').on("click", "a.deleteInsertRow",function () {
    $(this).parents("div.editorRow:first").remove();
    return false;
});
<script type="text/javascript">
$(document).ready(function () {
  $("#addItem").click(function () {
    $.ajax({
    url: this.href,
    cache: false,
    success: function (html) { $("#editorRows").append(html); }});
    return false;
  });
});
</script>