需要修复html和附加javascript的值 [英] need to fix html and values appending with javascript

查看:75
本文介绍了需要修复html和附加javascript的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要动态的表单,允许用户添加额外的结果,但无论他们需要多少措施,都需要与之关联,如下所示:



我遇到的问题是使结果和测量字段的名称正确。



我需要将第一个结果textarea命名为: outcome [1] 。 amd每个额外的结果textarea应该是结果[2],结果[3] 等等。



至于测量textareas,我需要他们这样命名: measure [1] [0],measure [1] [1],measure [1] [2] 等等

测量textarea的命名方式就像这样,因为它们在插入数据库时​​会循环运行,并且第一个索引号将与结果相关联。



因此,当用户添加更多措施textareas时,索引应该增加,例如...

结果[2],测量textarea: measure [2] [0],measure [2] [1] ,然后当他们点击插件标志时,它会创建一个新的测量textarea,名为 measure [2] [2]



接下来单击加号时也是如此结果,所以下一个结果textarea的名字应该是结果[2] ,点击另一个结果它会是结果[3] 等等。



代码有效,但我只需要有人看看它,并修改它,所以它有我想要的名称计划。

解决方案

好所以我修改了allot这里的代码是工作演示,通过制定儿童的评估结果,可以节省您在php方面的东西。



我拿出模板部分中的名字来阻止它们发布。



我复制了一个已经接近并清除了值的度量形式,因为不需要重新编号,因为它已经被正确命名了。



我也强烈建议你拿出br span和strong tags,那些应该用css或label来处理的东西是个好主意。

这里是html

 < form action =#method =post> 
< div class =outcomegroupid =template>
< div class =col>
< strong>结果#< span class =onum> 1< / span>< / strong>
< img class =plus addoutcomesrc =http://i54.tinypic.com/2zqzzo7.jpg/>
< img class =minus removeoutcomesrc =http://i53.tinypic.com/29apc8i.jpg/>
< br />
< textarea class =结果>< / textarea>
< / div>

< div class =col>

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

< div class =measure>
< textarea>< / textarea>
< img class =plus addmeasuresrc =http://i54.tinypic.com/2zqzzo7.jpg/>
< img class =minus removemeasuresrc =http://i53.tinypic.com/29apc8i.jpg/>
< / div>

< / div>

< / div>

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

和jquery在这里:

<$ p $每一个函数(索引,值){
$(this).find()函数重编号(){
$('#outcome-measure-fields .outcomegroup')。 ('.outcome')。attr('name','outcome ['+ index +'] [outcome]');
$(this).find('。measure textarea')。attr('name ''''results ['+ index +'] [measure] []');
$(this).find('。measure1 textarea')。attr('name','outcome ['+ 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();
重新编号()
});
$ b $('。removemeasure')。live('click',function(){
$(this).closest('。measure')。remove();
});
$ b $('#template')。clone()。appendTo($('#outcome-measure-fields'))。removeAttr('id');
renumber()

当然你的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;
职位:亲属;
top:-3px;
}

.minus {
display:none;
}

让我知道它是怎么回事


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

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

这篇关于需要修复html和附加javascript的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆