Javascript 如何删除使用jquery在运行时创建的文本框

Javascript 如何删除使用jquery在运行时创建的文本框,javascript,jquery,asp.net-mvc,Javascript,Jquery,Asp.net Mvc,我创建链接按钮,如果点击,它会自动添加带有删除链接的文本框。问题是在文本框和删除链接添加后,当单击删除链接时,它无法删除文本框。如何解决这个问题 看法 @使用(Html.BeginForm()) { @foreach(模型中的var项目) { Html.RenderPartial(“GiftEditorRow”,item); } @ActionLink(“添加另一个…”,“BlankEditorRow”,null,new{id=“addItem”}) } @节脚本{ $(文档).ready(函数

我创建链接按钮,如果点击,它会自动添加带有删除链接的文本框。问题是在文本框和删除链接添加后,当单击删除链接时,它无法删除文本框。如何解决这个问题

看法

@使用(Html.BeginForm())
{
@foreach(模型中的var项目)
{
Html.RenderPartial(“GiftEditorRow”,item);
}
@ActionLink(“添加另一个…”,“BlankEditorRow”,null,new{id=“addItem”})
}
@节脚本{
$(文档).ready(函数(){
$(“#添加项”)。单击(函数(){
$.ajax({
url:this.href,
cache:false,
成功:函数(html){
$(“#editorRows”).append(html);
}
});
返回false;
});
$(“a.deleteRow”)。在(“单击”,函数(){
$(this).parents(“div.editorRow:first”).remove();
返回false;
});
});
}
用于动态添加文本框的局部视图:

@model MvcApplication1.Models.Gift
@using MvcApplication1.Helpers

<div class="editorRow">
    @using (Html.BeginCollectionItem("gifts"))
    {
        <p>
            Item: @Html.TextBoxFor(x => x.Name)
            Value: @Html.TextBoxFor(x => x.Price, new { size = 4 })
            <a href="#" class="deleteRow">delete</a>
        </p>
    }
</div>
@model mvcapapplication1.Models.Gift
@使用MVCAPApplication1.Helpers
@使用(Html.BeginCollectionItem(“礼品”))
{

项目:@Html.TextBoxFor(x=>x.Name)
值:@Html.TextBoxFor(x=>x.Price,新的{size=4})

}
更换

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

替换

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


由于使用jquery动态生成HTML,因此必须使用事件委派

而不是

$("a.deleteRow").on("click", function () {
   $(this).parents("div.editorRow:first").remove();
   return false;
});
试一试


阅读更多内容

因为您使用jquery动态生成HTML,所以您必须使用事件委派

而不是

$("a.deleteRow").on("click", function () {
   $(this).parents("div.editorRow:first").remove();
   return false;
});
试一试


阅读更多

太好了。。。谢谢你的解释和链接,现在很清楚了。。。谢谢你的解释和链接,现在一切都清楚了
$(document).on("click", "a.deleteRow" ,function () {
   $(this).parents("div.editorRow:first").remove();
   return false;
});