【问题标题】:Jquery: Closest() row remove not workingJquery:最近()行删除不起作用
【发布时间】:2019-01-04 04:25:31
【问题描述】:

我有一个脚本,它添加一组行以允许您在 SharePoint 列表表单中为其他访问者捕获信息。我有两个锚标签,一个用于添加,另一个用于删除。当我添加一个新访问者时它可以工作,当我删除访问者时它仍然有效,但是如果我再次尝试添加它会带回之前删除的行加上新添加的行。有谁知道我该如何解决这个问题?

我写的脚本和源码:

function addVisitor(sender) {
    var newVisitor = "<tr class='newVisitor'><td nowrap='true' valign='top' width='113px' class='ms-formlabel'><span class='ms-h3 ms-standardheader' ><nobr>Visitor name</nobr></span></td><td valign='top' width='350px' class='ms-formbody'><span dir='none'><input type='text'  maxlength='255'  title='Visitor name' class='ms-long ms-spellcheck-true'><br></span></td></tr><tr><td nowrap='true' valign='top' width='113px' class='ms-formlabel'><span class='ms-h3 ms-standardheader' ><nobr>Visitor surname</nobr></span></td><td valign='top' width='350px' class='ms-formbody'><span dir='none'><input type='text'  maxlength='255'  title='Visitor surname' class='ms-long ms-spellcheck-true'><br></span></td></tr><tr><td nowrap='true' valign='top' width='113px' class='ms-formlabel'><span class='ms-h3 ms-standardheader' id='Visitor_x0020_email'><nobr>Visitor email*</nobr></span></td><td valign='top' width='350px' class='ms-formbody'><span dir='none'><input type='text' value='' maxlength='255'  title='Visitor email Required Field' style='ime-mode : ' class='ms-long ms-spellcheck-true'><br></span></td></tr><tr style='display:none;' class='vReg'><td nowrap='true' valign='top' width='113px' class='ms-formlabel'><span class='ms-h3 ms-standardheader' id='Vehicle_x0020_registration'><nobr>Vehicle registration</nobr></span></td><td valign='top' width='350px' class='ms-formbody'><span dir='none'><input type='text' value='' maxlength='255' title='Vehicle registration' style='ime-mode : ' class='ms-long ms-spellcheck-true'><br></span></td>" + addBtn + "</tr>";

    $(newVisitor).insertAfter(sender);
if (hideReg)
    $('.vReg').hide();
else
    $('.vReg').show();

$('.add').on('click', function () {
    var sender = $(this).closest('tr')[0];
    $(this).hide();
    addVisitor(sender);
    $('.add').hide();
    $('.add:last').show();

});

$('.remove').on('click', function () {
    var sender = $(this).closest('tr')[0];
    $(this).hide();
    $('.add').hide();
    removeVisitor(sender);
    $('.add:last').show();
});

}

function removeVisitor(sender) {
    var rowPos = $(sender).index();
    $(sender).remove(); // remove current
   // $('.ms-formtable tr').eq(rowPos - 1);
    for (var i = 1; i<=4; i++) {
        $($('.ms-formtable tr')[rowPos - i]).remove();
    }      

}

【问题讨论】:

  • 使用测试数据创建工作小提琴并共享 URL。然后我们可以检查。
  • 我认为 "$(this).closest('tr')[0]" 在这里不需要索引,当 jQuery 选择器只尝试使用 "$(this).closest('tr')"
  • 我建议您提供完整代码(HTML+JavaScript)以供进一步研究。
  • @LZ_MSFT 我更新了我的问题的代码。
  • @Hanif 我删除了,谢谢。

标签: javascript jquery html sharepoint-online


【解决方案1】:

您应该将事件附件移出addVisitor 方法。

$('.remove').on('click', function () { ... }

还有

$('.add').on('click', function () { ... }

否则,应用程序将出现错误行为,因为每次代码调用方法 addVisitor 时,它都会为事件添加一个新的处理程序,从而导致在用户单击元素时多次调用回调函数使用 .add.remove 类。

所以生成的代码是:

function addVisitor(sender) {
    var newVisitor = "<tr class='newVisitor'><td nowrap='true' valign='top' width='113px' class='ms-formlabel'><span class='ms-h3 ms-standardheader' ><nobr>Visitor name</nobr></span></td><td valign='top' width='350px' class='ms-formbody'><span dir='none'><input type='text'  maxlength='255'  title='Visitor name' class='ms-long ms-spellcheck-true'><br></span></td></tr><tr><td nowrap='true' valign='top' width='113px' class='ms-formlabel'><span class='ms-h3 ms-standardheader' ><nobr>Visitor surname</nobr></span></td><td valign='top' width='350px' class='ms-formbody'><span dir='none'><input type='text'  maxlength='255'  title='Visitor surname' class='ms-long ms-spellcheck-true'><br></span></td></tr><tr><td nowrap='true' valign='top' width='113px' class='ms-formlabel'><span class='ms-h3 ms-standardheader' id='Visitor_x0020_email'><nobr>Visitor email*</nobr></span></td><td valign='top' width='350px' class='ms-formbody'><span dir='none'><input type='text' value='' maxlength='255'  title='Visitor email Required Field' style='ime-mode : ' class='ms-long ms-spellcheck-true'><br></span></td></tr><tr style='display:none;' class='vReg'><td nowrap='true' valign='top' width='113px' class='ms-formlabel'><span class='ms-h3 ms-standardheader' id='Vehicle_x0020_registration'><nobr>Vehicle registration</nobr></span></td><td valign='top' width='350px' class='ms-formbody'><span dir='none'><input type='text' value='' maxlength='255' title='Vehicle registration' style='ime-mode : ' class='ms-long ms-spellcheck-true'><br></span></td>" + addBtn + "</tr>";

    $(newVisitor).insertAfter(sender);
    if (hideReg)
        $('.vReg').hide();
    else
        $('.vReg').show();       
}

function removeVisitor(sender) {
    var rowPos = $(sender).index();
    $(sender).remove(); // remove current
   // $('.ms-formtable tr').eq(rowPos - 1);
    for (var i = 1; i<=4; i++) {
        $($('.ms-formtable tr')[rowPos - i]).remove();
    }            
}


$('.add').on('click', function () {
    var sender = $(this).closest('tr')[0];
    $(this).hide();
    addVisitor(sender);
    $('.add').hide();
    $('.add:last').show();

});

$('.remove').on('click', function () {
    var sender = $(this).closest('tr')[0];
    $(this).hide();
    $('.add').hide();
    removeVisitor(sender);
    $('.add:last').show();
});

【讨论】:

    猜你喜欢
    • 2011-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多