当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Rearrange array in jquery

问题描述:

I have a problem currently i do following

Html

<input type="text" name="Answers[0]" class="ahsan required" ><a href="#">Remove</a>

<input type="text" name="Answers[1]" class="ahsan required" ><a href="#">Remove</a>

<input type="text" name="Answers[2]" class="ahsan required" ><a href="#">Remove</a>

<input type="text" name="Answers[3]" class="ahsan required" ><a href="#">Remove</a>

<input type="text" name="Answers[4]" class="ahsan required" ><a href="#">Remove</a>

<input type="text" name="Answers[5]" class="ahsan required" ><a href="#">Remove</a>

above inputs are dynamically generated and obviously if user can add row then he can also delete row but if he delete row from center and submit form i am unable to get all text boxes value in array . Now i want that when i click on remove of any text box then text box gets remove but also all remaining text boxes should reordered . Can i do this Please help me . Thanks in advance

网友答案:

unless yo're using it numeric indexes for other purposes, just try to remove them from name, having rows like

<input type="text" name="Answers[]"...

so there's no need to reorder your inputs. You should be also able to get an array on server-side processing

otherwise everytime you update the DOM use something like

$(".ahsan").each(function(i){
    $(this).attr({name : "Answer[" + i + "]"});
})
网友答案:
function reArrangeItems(){
    var i = 0;
    $(".ahsan").each(function(){
       $(this).attr({name : "Answer[" + (i++) + "]"});
    })
}

call this function after insert and delete

分享给朋友:
您可能感兴趣的文章:
随机阅读: