简体   繁体   English

更改Jquery Clone中的复杂ID值

[英]Change complex id value in Jquery Clone

This is my input field 这是我的输入字段

<input class="form-control" id="StudentInstitutes_0__Name" name="StudentInstitutes[0].Name" value="" type="text">

I use clone() to the div wrapping the above input field. 我将clone()用于包装上述输入字段的div。 That works fine. 很好

But how can I change the id & name index which is "0" in above with the button click. 但是,如何通过单击按钮更改上面的ID和名称索引为“ 0”。

Ex: if I click button for the first time the input field will be: 例如:如果我第一次单击按钮,输入字段将是:

<input class="form-control" id="StudentInstitutes_1__Name" name="StudentInstitutes[1].Name" value="" type="text">

This is my button 这是我的按钮

<div class="form-group">
    <div class="col-sm-6">
        <input type="button" name="ShowMoreInstitution" id="ShowMore" value="Add More Institution" onclick="javascript:  showMoreInstitution();" />
    </div>                                       
</div>

This is my JS 这是我的JS

$("#showMore").click(function () {
    $('#showInstitute').clone().insertAfter("#showInstitute");
});

Note that I am using Asp.net Mvc Razor view and don't want to use partial view for the operation. 请注意,我正在使用Asp.net Mvc Razor视图,并且不想对该操作使用部分视图。

As I come to know that you wants to change id dynamically, so you have to first declare global variable "value" and assign initial value to it 0. 据我所知,您想动态更改id,因此必须首先声明全局变量“ value”并为其分配初始值0。

$("#showMore").click(function () {
    value++;
    $('#showInstitute').removeAttr('id').attr('id', 'StudentInstitutes_" + value + "__Name');
});

this code may help you. 此代码可以为您提供帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM