开发者

need to fix html and values appending with javascript [duplicate]

开发者 https://www.devze.com 2023-02-28 03:35 出处:网络
This question already has an answer here: need to display extra text inputs when selecting add button, also need to remove fields
This question already has an answer here: need to display extra text inputs when selecting add button, also need to remove fields (1 answer) 开发者_StackOverflow Closed 7 years ago.

I have a form that needs to be dynamic, allowing the user to add extra outcomes, w/ however many measures they need associated with it, as depicted here: http://i.stack.imgur.com/EMcoa.png

Here is a sketch of the naming scheme: http://i55.tinypic.com/2s6rh4y.png

need to fix html and values appending with javascript [duplicate]

Below is the code in some form of working order: http://jsfiddle.net/bkmorse/yNZcD/2/

What I am having a problem with is making the name of the outcome and measure fields correct.

I need the first outcome textarea to be named: outcome[1]. amd every extra outcome textarea should be outcome[2], outcome[3] and so on.

As for the measure textareas, I need them be named like this: measure[1][0], measure[1][1], measure[1][2] and so on.

The measure textarea's are named like that, because they will be ran thru a loop when inserted into the database, and the first index number will relate to the outcome.

So when the user adds more measure textareas, the index should increase, for example...

outcome[2], has measure textarea: measure[2][0], measure[2][1], then when they click the plug sign, it will create a new measure textarea, named measure[2][2].

Same thing goes for when clicking the plus sign next to outcome, so the next outcome textarea name would be outcome[2], click for another outcome it would be outcome[3] and so on.

The code works, but I just need to have someone look at it and revise it so it has the name scheme I want.


ok so I revised allot of code here is the WORKING DEMO by making the measures children of the outcome it will save you parsing in the php side of things.

I took out the names in the template portion to prevent them from posting.

I copy the measure form one that's already close by and clear the value since there is no need to renumber everything since it will already be named correctly.

I would also strongly suggest you take out the br span and strong tags those things should be handled with css or label would be a good idea

here is the html

<form action="#" method="post">
    <div class="outcomegroup" id="template">
        <div class="col">
            <strong>Outcome #<span class="onum">1</span></strong>
            <img class="plus addoutcome" src="http://i54.tinypic.com/2zqzzo7.jpg" />
            <img class="minus removeoutcome" src="http://i53.tinypic.com/29apc8i.jpg" />
            <br />
            <textarea class="outcome"></textarea>
        </div>

        <div class="col">

            <div class="measure1">
                <textarea></textarea>
            </div>

            <div class="measure">
                <textarea></textarea>
                <img class="plus addmeasure" src="http://i54.tinypic.com/2zqzzo7.jpg"/>
                <img class="minus removemeasure" src="http://i53.tinypic.com/29apc8i.jpg"/>
            </div>

        </div>

    </div>

    <div id="outcome-measure-fields"></div>
    <input type="submit" value="submit" />
</form>

and the jquery here:

function renumber() {
    $('#outcome-measure-fields .outcomegroup').each(function(index, value) {
        $(this).find('.outcome').attr('name', 'outcomes[' + index + '][outcome]');
        $(this).find('.measure textarea').attr('name', 'outcomes[' + index + '][measure][]');
        $(this).find('.measure1 textarea').attr('name', 'outcomes[' + index + '][measure][]');
        $(this).find('.onum').text(index + 1);
    });

}

$('.addmeasure').live('click', function() {
    $(this).closest('.measure').clone().insertAfter($(this).closest('.measure')).val('').find('.minus').show();
});

$('.addoutcome').live('click', function() {
    $('#template').clone().removeAttr('id').insertAfter($(this).closest('.outcomegroup')).find('.minus').show();
    renumber();
});

$('.removeoutcome').live('click', function() {
    $(this).closest('.outcomegroup').remove();
    renumber()
});

$('.removemeasure').live('click', function() {
    $(this).closest('.measure').remove();
});

$('#template').clone().appendTo($('#outcome-measure-fields')).removeAttr('id');
renumber()

and of course your css

.outcomegroup {
    overflow: auto;  
}

#template {
    display: none;
}

.col {
    float: left;
    margin: 5px;  
    font-size: 20px;
    font-weight: bold;
}

.plus, .minus {
    width: 20px;
    margin: 2px;
    vertical-align: middle;
    position: relative;
    top: -3px;
}

.minus {
    display: none;   
}

let me know how it goes


granted mcgrailm's solution is perfectly valid, may i suggesting using a framework called knockoutjs? it's designed for exactly what you're doing; see this example.

0

精彩评论

暂无评论...
验证码 换一张
取 消