简体   繁体   中英

Clone HTML field on field select

I am trying to clone a part of my HTML search for when i select another field .

Full Html form is :

<div class="form-group col-sm-6 col-md-3">
<h4 class="title">Who</h4>
<div class="row">
    <div class="col-xs-4">
        <label>Rooms</label>
        <div class="selector">
            <select name="roomsno" class="full-width">
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
                <option value="4">4</option>
            </select>
        </div>
    </div>
    <!-- FROM HERE -->
    <div class="col-xs-4">
        <label>Adults</label>
        <div class="selector">
            <select name="adults" class="full-width">
                <option value="1">1</option>
                <option value="2" selected="selected">2</option>
                <option value="3">3</option>
                <option value="4">4</option>
            </select>
        </div>
    </div>
    <div class="col-xs-4">
        <label>Kids</label>
        <div class="selector">
            <select name="kids" class="full-width">
                <option value="0">0</option>
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
            </select>
        </div>
    </div>
</div>
<div class="age-of-children no-display">
    <h5>Kids Age</h5>
    <div class="row">
        <div class="col-xs-4 child-age-field">
            <label>Kid 1</label>
            <div class="selector validation-field">
                <select name="child_ages[]" class="full-width">
                    <option value="0">0</option>
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                    <option value="4">4</option>
                    <option value="5">5</option>
                    <option value="6">6</option>
                    <option value="7">7</option>
                    <option value="8">8</option>
                    <option value="9">9</option>
                    <option value="10">10</option>
                    <option value="11">11</option>
                    <option value="12">12</option>
                    <option value="13">13</option>
                    <option value="14">14</option>
                    <option value="15">15</option>
                    <option value="16">16</option>
                    <option value="17">17</option>
                </select>
            </div>
        </div>
    </div>
</div>
<!-- To here-->
</div>

What i need is when i select two or more rooms to clone the area that is commented From here - To here

I have arleady a similar code but is for kids age The cpde that o have for kids age is:

    // handle kid age filter
tjq('select[name=kids]').change(function(){
    var prev_kids = tjq('.age-of-children .child-age-field').length;
    tjq('.age-of-children').removeClass('no-display');
    var i;
    if (prev_kids > tjq(this).val()) {

        var current_kids = tjq(this).val();

        if (current_kids == 0) {
            current_kids = 1;
            tjq('.age-of-children').addClass('no-display');
        }

        for (i = prev_kids; i > current_kids; --i) {
            tjq('.age-of-children .child-age-field').eq(i-1).remove();
        }
    } else {
        for (i = prev_kids + 1; i <= tjq(this).val(); i++) {
            var clone_age_last = tjq('.age-of-children .child-age-field:last').clone();
            var clone_age = clone_age_last.clone();
            tjq('.age-of-children .row').append(clone_age);
            var name = clone_age.find('label').text().replace(/(\d+)/, function(match, p1)
            {
                return (parseInt(p1) + 1);
            });
            clone_age.find('label').text(name);
            clone_age.find('select').val(0);
            clone_age.find('.custom-select').text(0);
        }
    }
});

Hope to get this sorted out,

i would do it that way:

1) wrap an additional div arround your "from here/to here" eg with the class "additionalRoom"

2) change event on your room number select and get the number with $(this).val(); ( http://api.jquery.com/change/ )

3) copy your additionalRoom div with clone() and insert it x times based on the chosen rooms number at the wanted position ( http://api.jquery.com/clone/ )

4) and i would change the room form part that way that the same html could simply be cloned and resulting the form data in a good way. eg name="roomAdults[]", name="roomKids[]" or you have the room data as numbered array (room[1][adults], room[2][kids]). in the last case you have to increase this number after cloning and before inserting

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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