简体   繁体   English

动态添加下拉列表和下拉字段

[英]Dynamically add dropdown and dropdown fields

EDIT 001: 编辑001:

added: - event.preventDefault(); 添加: event.preventDefault(); and anchors instead of buttons 和锚点而不是按钮

<div class="edit-area">
 <div class="controls">
    <a href="#" class="add">+</a>
    <a href="#" class="rem">-</a>
 </div>
</div>

Now the page + or - won't make the page send the form, but now it does nothing :s 现在页面+-不会使页面发送表单,但是现在它什么都不做:s


I'm working on a contact form, but I have a few problems/questions. 我正在使用联系表格,但是我有一些问题。

  • I want to dynamically add fields when they press a button (+) 我想在按下按钮(+)时动态添加字段
  • And off course they need to be able to delete this when needed... (-) 当然,他们需要能够在需要时将其删除...(-)

When I press the + button now, it tries to act like a submit button and sends the form... 当我现在按+按钮时,它会尝试像“提交”按钮并发送表格...

Script 脚本

<script>
        (function($) {
            "use strict";

            var itemTemplate = $('.example-template').detach(),
                editArea = $('.edit-area'),
                itemNumber = 1;

            $(document).on('click', '.edit-area .add', function(event) {
                var item = itemTemplate.clone();
                item.find('[project]').attr('project', function() {
                return $(this).attr('project') + '_' + itemNumber;
            });
            ++itemNumber;
            item.appendTo(editArea);
        });

        $(document).on('click', '.edit-area .rem', function(event) {
            editArea.children('.example-template').last().remove();
        });

        $(document).on('click', '.edit-area .del', function(event) {
            var target = $(event.target),
            row = target.closest('.example-template');
            row.remove();
        });

    }(jQuery));
</script>

HTML HTML

<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="POST" name="contact">
    <div class="row uniform 50%">
        <div class="6u 12u(mobilep)">
            Your personal card number
            <input type="text" name="card2" id="card" value="<?php echo $_SESSION['username']; ?>" placeholder="Card Number" readonly/>
        </div>
        <div class="6u 12u(mobilep)">
            Your name
            <input type="text" name="name2" id="name" value="<?php echo $_SESSION['realName']; ?>" placeholder="Your name" readonly/>
        </div>
    </div>
    <div class="row uniform 50%">
            <div class="6u 12u(narrower)">
                Order tickets for a project.
            </div>
    </div>
<div class="example-template">
    <div class="row uniform 50%" id="readroot">
            <div class="4u 12u(narrower)">
            <select name="project" id="ddl" onchange="configureDropDownLists(this,document.getElementById('ddl2'))">
            <option disabled selected>Select a project</option>
                <option value="Smile">Project Smile</option>
                <option value="Sand">Project Sand</option>
                <option value="Schmuck">Project Schmuck</option>
            </select>
            </div>
            <div class="4u 12u(narrower)">
            <select id="ddl2" name="date">
                    <option disabled selected>Select a date</option>
            </select>
            </div>
            <div class="2u 12u(narrower)">
            <input type="text" onkeypress='return event.charCode >= 48 && event.charCode <= 57' name="amount" id="currentamount" value="" placeholder="Amount" />
            </div>
        </div>  
        <div class="edit-area">
        <div class="controls">
            <button class="add">+</button>
            <button class="rem">-</button>
        </div>
    </div>
    <div class="row uniform 50%">
        <div class="6u 12u(mobilep)">
            <input type="hidden" id="currentprice" value="10" />
        </div>
    </div>          
        <div class="6u 12u(mobilep)">
            <input type="hidden" id="nextprice1" value="10" placeholder="" />
        </div>
    </div>
    <div class="row uniform">
        <div class="12u">
        </div>
    </div>
        <div class="4u 12u(mobilep)">   
        Total price.(In EUR)
        <input type="text" name="total2" id="total" value=""  readonly/>
    </div>

    <div class="row uniform">
        <div class="12u">
            <ul class="actions align-center">
                <li><input type="submit" name="submit"value="Place Order"/></li>
            </ul>
        </div>
    </div>
</form>

Image of the form (id and name are not shown here) 表格图片(此处未显示ID和名称)

在此处输入图片说明

Change your buttons to anchors: 将按钮更改为锚点:

<a href="#" class="add">+</a>
<a href="#" class="rem">-</a>

And then in your javascript, when clicked, be sure to call preventDefault() to stop it from behaving like a standard link. 然后在您的javascript中,单击时,请确保调用preventDefault()以阻止其行为像标准链接一样。

$(document).on('click', '.edit-area .rem', function(event) {
    event.preventDefault();
    editArea.children('.example-template').last().remove();
});

$(document).on('click', '.edit-area .del', function(event) {
    event.preventDefault();
    var target = $(event.target),
    row = target.closest('.example-template');
    row.remove();
});

I think alternatively, you can add a type to the button so it doesn't default to a submit type (but don't quote me on that): 我认为,您也可以在按钮上添加一种type ,这样它就不会默认为submit类型(但不要在其上引用我):

<button type="button" class="add">+</button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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