簡體   English   中英

如何在克隆時更改輸入字段的ID和名稱

[英]How to change the id and name of the input fields on cloning

嗨,我正在利用以下克隆示例

HTML代碼示例

  <form method="post" action="#" class="inlineForm" enctype="multipart/form-data">
    <div class="repeatingSection">
        <a href="#" class="buttonGray buttonRight deleteFight">Delete</a>
        <input type="hidden" name="fighter_a_id_1" id="fighter_a_id_1" value="" />
        <input type="hidden" name="fighter_b_id_1" id="fighter_b_id_1" value="" />
        <input type="hidden" name="winner_id_1" id="winner_id_1" value="" />
        <div class="formRow">
            <label for="fighter_a_1">Fighters</label>
            <input type="text" name="fighter_a_1" id="fighter_a_1" value="" /> <span class="formTextExtraCenter">vs</span> <input type="text" name="fighter_b_1" id="fighter_b_1" value="" />
        </div>
        <div class="formRow">
            <label for="fighter_a_pay_1">Fighter Pay $</label>
            <input type="text" name="fighter_a_pay_1" id="fighter_a_pay_1" value="" /> <span class="formTextExtraCenter">vs</span> <input type="text" name="fighter_b_pay_1" id="fighter_b_pay_1" value="" />
        </div>
        <div class="formRow">
            <label for="winner_1">Winner</label>
            <input type="text" name="winner_1" id="winner_1" value="" />
        </div>
        <div class="formRow">
            <label for="method_1">Method</label>
            <input type="text" name="method_1" id="method_1" value="" />
        </div>
        <div class="formRow">
            <label for="method_type_1">Method Type</label>
            <input type="text" name="method_type_1" id="method_type_1" value="" />
        </div>
        <div class="formRow">
            <label for="round_1">Round</label>
            <input type="text" name="round_1" id="round_1" class="fieldSmall" value="" />
        </div>
        <div class="formRow">
            <label for="time_1">Time</label>
            <input type="text" name="time_1" id="time_1" class="fieldSmall" value="" />
        </div>
        <div class="formRow">
            <label for="fight_number_1">Fight #</label>
            <input type="text" name="fight_number_1" id="fight_number_1" class="fieldSmall" value="" />
        </div>
    </div>
    <div class="formRowRepeatingSection">
            <a href="#" class="buttonGray buttonRight addFight">Add Fight</a>
        </div>
    <div class="formRow">
        <input type="submit" class="submitButton" value="Save Fights" />
    </div>
</form>

JS代碼

<script type="text/javascript">
    // Add a new repeating section
    var attrs = ['for', 'id', 'name'];

    function resetAttributeNames(section) {
        var tags = section.find('input, label'), idx = section.index();
        tags.each(function () {
            var $this = $(this);
            $.each(attrs, function (i, attr) {
                var attr_val = $this.attr(attr);
                if (attr_val) {
                    $this.attr(attr, attr_val.replace(/_\d+$/, '_' + (idx + 1)))
                }
            })
        })
    }

    $('.addFight').click(function (e) {
        e.preventDefault();
        var lastRepeatingGroup = $('.repeatingSection').last();
        var cloned = lastRepeatingGroup.clone(true)
        cloned.insertAfter(lastRepeatingGroup);
        resetAttributeNames(cloned)
    });

    // Delete a repeating section
    $('.deleteFight').click(function (e) {
        e.preventDefault();
        var current_fight = $(this).parent('div');
        var other_fights = current_fight.siblings('.repeatingSection');
        if (other_fights.length === 0) {
            alert("You should atleast have one fight");
            return;
        }
        current_fight.slideUp('slow', function () {
            current_fight.remove();

            // reset fight indexes
            other_fights.each(function () {
                resetAttributeNames($(this));
            })

        })


    });



</script>

輸入字段name="fighter_a_id_1"類似於name="fighter_a_id_1" ,而替換表達式為$this.attr(attr, attr_val.replace(/_\\d+$/, '_' + (idx)))

我想要輸入字段名稱,例如name="Jobs[0].AssignedTo"並且我想增加數字,所以如何編寫表達式,我需要這樣的名稱,因為asp.net mvc需要它

所以請幫我...!

嘗試這個

您可以使用data- *屬性作為名稱和ID模板,而在克隆該元素時,可以將其替換為所需的數字

 function AddControl(){ var inpt = $(".dummyinput").clone(); var IdCount = 0; $(inpt).addClass("workinginput").removeClass("dummyinput"); IdCount = $(".workinginput").length; $(inpt).attr("id",$(inpt).data("id").replace("__ID__",IdCount)); $(inpt).attr("name",$(inpt).data("name").replace("__NAME__",IdCount)); $("div").append($(inpt)); } AddControl(); AddControl(); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type='text' class='dummyinput' data-id='controlId[__ID__]' data-name='controlName[__NAME__]'> <div> </div> 

這對我有用

$this.attr(attr, attr_val.replace(/\[\d\]/,'['+(idx)+']'))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM