创建动态 html 表单

发布于 2024-10-19 21:49:51 字数 381 浏览 1 评论 0原文

我想创建一个动态变化的表单。

我有一个用于创建项目的表单(包含以下字段:project_name、project_description...),并且该项目可以具有任意数量(大于或等于 0)的类别。

我想要的是显示一个按钮,让用户可以选择添加另一个类别字段。此外,我还希望用户可以“删除”类别字段的选项(如果他改变主意或犯了错误)。最好的方法是什么?我想要一个 Ajax 类型的解决方案。

到目前为止,我的解决方案是在最后一个类别下留下一个空的 div,然后单击按钮将另一个字段加载到该 div 中,其中还有另一个 div 将用于下一个 div。对这个解决方案不满意,因为我现在必须计算我有多少个字段,并为每个 div 提供它自己的 id,这使问题更加复杂。

有没有更简单的解决方案?

I would like to create a form that changes dynamically.

I have a form for creating a project (with fields such as: project_name, project_description...) and the project can have any amount (bigger or equal to 0) of categories.

What i want is to display a button which would give the user the option to add another category field. In addition I would also like the option for category fields to be "deleteable" by the user (if he changes his mind or made a mistake). What would be the best way to do so. I would like an Ajax type solution.

My solution so far is to leave an empty div beneath the last category and onclick of the button to load another field into that div with yet another div which will be used for the next div. Not to happy with this solution since i now have to count how many fields I have and give each div it's own id which complicates the matter even more.

Is there a more simple solution to this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

跨年 2024-10-26 21:49:51

如果您尝试使用按钮动态添加字段,则可以通过执行以下操作轻松实现:

HTML:

<form>
    <p>
        <label>Name:</label> <input type="text">
        <label>Age:</label> <input type="text">
        <span class="remove">Remove</span>
    </p>
    <p>
        <span class="add">Add fields</span>
    </p>
</form>

JS:

$(".add").click(function() {
    $("form > p:first-child").clone(true).insertBefore("form > p:last-child");
    return false;
});

$(".remove").click(function() {
    $(this).parent().remove();
});

演示:http://jsfiddle.net/UeSsu/1/

If you are trying to add fields dynamically with a button, you can easily do so by doing something like the following:

HTML:

<form>
    <p>
        <label>Name:</label> <input type="text">
        <label>Age:</label> <input type="text">
        <span class="remove">Remove</span>
    </p>
    <p>
        <span class="add">Add fields</span>
    </p>
</form>

JS:

$(".add").click(function() {
    $("form > p:first-child").clone(true).insertBefore("form > p:last-child");
    return false;
});

$(".remove").click(function() {
    $(this).parent().remove();
});

Demo: http://jsfiddle.net/UeSsu/1/

≈。彩虹 2024-10-26 21:49:51

不久前我开始编写一个基于 JSON 定义的表单生成器。它可以工作,但可以使用一些增强功能。它是使用 Prototype.js 编写的,但将其移植到 jQuery 并不需要付出巨大的努力。

欢迎您窃取代码。 (只需查看源代码)

I started to write a form generator is based on a definition in JSON a while back. It works but could use some enhancements. It's written using Prototype.js but it wouldn't be a huge effort to port it over to jQuery.

You're welcome to steal the code. (just view source)

诗酒趁年少 2024-10-26 21:49:51

我也做过类似的事情。要删除字段,我并没有真正删除字段。我只是用 display:none 隐藏了它们,并有一个隐藏的 input “删除”,我将其触发为 true。然后,接收结果的页面就知道要删除数据库中的哪个字段。

在提交表单之前它们不会被删除。这就像“两次通过”的概念。但如果您并不真正需要真正的 ajax,它也可以正常工作。否则,您需要 JS 删除函数来调用服务器并告诉删除带有其 id 的字段。代码稍微复杂一些。

I've done something similar. To delete fields I didn't really removed fields. I just hidden them with a display:none and had a hidden input "delete" that I trigger to true. Then, the page receiving the result knows which field is to be deleted in the database.

They are not deleted before the form is submitted. It's like a "two pass" conception. But if you don't really need a true ajax, it works fine. Otherwise you need your JS remove function to call the server and tell to delete the field with its id. A little bit more complex to code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文