使用Jquery重命名JSP标记

使用Jquery重命名JSP标记,jquery,jsp,Jquery,Jsp,帮助支持使用JSP和Jquery的项目。他们将输入框命名为数组,然后将表单序列化为JSON。问题是他们在表单上有一个删除按钮,并且在提交之前没有重命名输入框。JAVA代码将其视为数组中的空行。是否有某种方法可以循环这些元素并在Jquery中重命名(重新编号) HTML 是的,您可以随时获取这些元素并更新其名称属性;请参阅内联注释 deleteThisAssigneeFromSet : function(btn, remove) { // Get the buttons' parent `

帮助支持使用JSP和Jquery的项目。他们将输入框命名为数组,然后将表单序列化为JSON。问题是他们在表单上有一个删除按钮,并且在提交之前没有重命名输入框。JAVA代码将其视为数组中的空行。是否有某种方法可以循环这些元素并在Jquery中重命名(重新编号)

HTML


是的,您可以随时获取这些元素并更新其
名称
属性;请参阅内联注释

deleteThisAssigneeFromSet : function(btn, remove) {
    // Get the buttons' parent `tr` (I'd use `closest`, not `parents`)
    var parent = $(btn).closest('tr');
    if( ! parent.hasClass('pte_row_markup') ) {
        // Get the row's siblings
        var siblings = parent.siblings('tr');
        // Remove the row
        parent.remove();
        // Update the inputs in the rows, row-by-row
        siblings.each(function(i) {
            var brackets = "[" + i + "]";
            // Uses an "attribute starts with" selector to find the inputs
            $(this).find("[name^=assignmentSetGridList]").each(function() {
                this.name = this.name.replace(/\[\d+\]/, brackets);
            });
        });
    }
实例:

函数deleteThisAssigneeFromSet(btn,删除){
//获取按钮'parent'tr'(我会使用'closest',而不是'parents')
var parent=$(btn).closest('tr');
if(!parent.hasClass('pte_行\u标记')){
//得到这一排的兄弟姐妹
var sibles=parent.sibles('tr');
//删除该行
parent.remove();
//逐行更新行中的输入
兄弟姐妹。每个(功能(i){
var方括号=“[”+i+“]”;
//使用“属性以开头”选择器查找输入
$(this).find(“[name^=assignmentSetGridList]”)。每个(函数(){
this.name=this.name.replace(/\[\d+\]/,括号);
});
});
}
}
$(“输入[value=delete]”)。在(“单击”,函数(){
删除ThisAssigneeFromSet(本);
});


(啊!如果您没有看到上面的
replace
,请点击refresh。)似乎不起作用。DOM中的编号仍然为奇数。从[0]到[2]不应该是这样的:deleteThisAssigneeFromSet:function(btn,remove){if(!$(btn).parents('tr').hasClass('pte_row_markup')){$(btn.).parents('tr').remove();$(btn.).closest('table”).find('tr”).each(函数(i){this.name=this.name.replace(/[\d+]/,“[+i+]”;))}@ssgtob1-我应该一行一行地使用行索引。我已经修复了它,很抱歉。先生,你太棒了。非常好用!
deleteThisAssigneeFromSet : function(btn, remove) {

        if( ! $(btn).parents('tr').hasClass('pte_row_markup') ) {
            $(btn).parents('tr').remove();
        }






    }
deleteThisAssigneeFromSet : function(btn, remove) {
    // Get the buttons' parent `tr` (I'd use `closest`, not `parents`)
    var parent = $(btn).closest('tr');
    if( ! parent.hasClass('pte_row_markup') ) {
        // Get the row's siblings
        var siblings = parent.siblings('tr');
        // Remove the row
        parent.remove();
        // Update the inputs in the rows, row-by-row
        siblings.each(function(i) {
            var brackets = "[" + i + "]";
            // Uses an "attribute starts with" selector to find the inputs
            $(this).find("[name^=assignmentSetGridList]").each(function() {
                this.name = this.name.replace(/\[\d+\]/, brackets);
            });
        });
    }