0

我有一个如下表格。

    <div class="container">
    <form method="post" action="index.php">
        <div id="rule">
            <div class="row">
                <div class="span5">
                    <input type="text" class="input-xlarge" value="Rule Name"/>
                </div>
                <div class="span6">
                    <input type="text" class="input-xxlarge" value="Description"/>
                </div>
                <div class="clearfix"> </div>
            </div>
        </div>
        <br />
        <div id="sel" class="select">
            <div class="row">
                <div class="span1 offset2">
                    <p class="btn btn-danger">If</p>
                </div>
                <div class="span2">
                   <select class="span2" name="metrics">
                      <option value="bounce_rate">Bounce Rate</option>
                      <option value="location">Locations</option>
                      <option value="social_media">Social Media</option>
                      <option value="search">Search</option>
                      <option value="visits">Visitors</option>
                    </select>
                </div>
                <div class="span1">
                  <select name="conditions" class="span2">
                       <option value=">">></option>
                       <option value="<"><</option>
                       <option value=">=">>=</option>
                       <option value="<="><=</option>
                        <option value="=">=</option>
                     </select>
                </div>
                <div class="span1 offset1">
                    <input type="text" name="percentage" class="input-small"/>
                </div>
                <div class="clearfix"> </div>
            </div>
            <br />
            <div class="row">
                <div class="span1 offset2">
                    <p class="btn btn-danger">Period</p>
                </div>
                <div class="span3">
                    <div class="input-append date datepicker" data-date="12-02-2012" data-date-format="dd-mm-yyyy">
                      <input class="span2" size="16" type="text" value="12-02-2012" />
                      <span class="add-on"><i class="icon-th"></i></span>
                    </div>
                </div>
                <div class="span3">
                    <div class="input-append date datepicker" data-date="12-02-2012" data-date-format="dd-mm-yyyy">
                      <input class="span2" size="16" type="text" value="12-02-2012" />
                      <span class="add-on"><i class="icon-th"></i></span>
                    </div>
                </div>
                <div class="clearfix"> </div>
            </div>
        </div> 
        <div id="dyna"></div>
        <div id="end">
            <div class="row">
                <div class="span1">
                    <p class="btn btn-danger">THEN</p>
                </div>
                <div class="span3">
                    <input type="text" class="input-xlarge" value="Statement" />
                </div>
                <div class="clearfix"> </div>
            </div>
            <div class="row">
                <div class="span1">
                    <p class="btn btn-danger">ELSE</p>
                </div>
                <div class="span3">
                    <input type="text" class="input-xlarge" value="Statement" />
                </div>
                <div class="clearfix"> </div>
            </div>
            <div class="row">
                <button class="span1 btn btn-success" type="submit" value="submit">Submit</button>
                <div class="span2 offset9">
                    <p class="btn btn-success input-small" id="addRule">Add Rule </p>
                </div>
                <div class="clearfix"> </div>
            </div>
        </div>
    </form>     
</div>

我在上面添加了一个“添加规则”按钮来创建动态元素。所以我使用了一个 javascript 来创建与上面相同的动态元素。javascript如下。

<script type="text/javascript">
$('.datepicker').datepicker();
(function(){
    var i = 0;
    $("#addRule").live('click',function(){
        ++i;
        var $ctrl = $('<div id="'+ i +'" class="select"><div class="row"><div class="span1 offset2"><button class="btn btn-danger">If</button></div><div class="span2"><select class="span2" name="metrics"><option value="bounce_rate">Bounce Rate</option><option value="location">Locations</option><option value="social_media">Social Media</option><option value="search">Search</option><option value="visits">Visitors</option></select></div><div class="span1"><select name="conditions" class="span2"><option value=">">></option><option value="<"><</option><option value=">=">>=</option><option value="<="><=</option><option value="=">=</option></select></div><div class="span1 offset1"><input type="text" name="percentage" class="input-small"/></div><div class="span2 offset1"><button class="btn btn-danger remove" id="'+ i +'">Remove</button></div><div class="clearfix"> </div></div><br /><div class="row"><div class="span1 offset2"><p class="btn btn-danger">Period</p></div><div class="span3"><div class="input-append date datepicker" data-date="12-02-2012" data-date-format="dd-mm-yyyy"><input class="span2" size="16" type="text" value="12-02-2012" /><span class="add-on"><i class="icon-th"></i></span></div></div><div class="span3"><div class="input-append date datepicker" data-date="12-02-2012" data-date-format="dd-mm-yyyy"><input class="span2" size="16" type="text" value="12-02-2012" /><span class="add-on"><i class="icon-th"></i></span></div></div><div class="clearfix"> </div></div></div>');
        $("#dyna").append($ctrl);
        $('.datepicker').datepicker();
}); 
}) ();

(function(){
    $(".remove").live('click',function(){
        var remDiv = $(this).attr('id');
        //var questionList = "questionList"+remQ;
        $("#" + remDiv).remove();
});

}) ();

所以我想在单击提交按钮后获取值。我只使用一个表单来获取数据是正确的方式,还是我分别创建两个表单?那么该怎么做呢?

4

1 回答 1

1

如果您将新元素插入到现有表单中,并且只要它们与您想要接收的任何现有输入具有不同的名称,则不需要创建第二个表单。

如果您想为两个不同的输入数据集合使用不同的提交按钮,您可以创建第二个表单。

于 2012-08-07T04:45:59.260 回答