添加一个字段到表单(jQuery的) [英] Adding a field to a form (jQuery)

查看:775
本文介绍了添加一个字段到表单(jQuery的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在构造形式。这里没有问题。除了一个提交按钮,我还有一个按钮,点击后,增加了一个字段的形式。我试图使用JavaScript(jQuery的)来做到这一点。不用说,我没有任何运气。
我已经尝试了许多不同的方法,这是一个我放弃了在:

I'm constructing a form. No problem there. Aside of a submit button, I have another button that, when clicked, adds another field to the form. I am attempting to use javascript (jquery) to do this. Needless to say I am not having any luck. I have tried many different methods, and this is the one I gave up at:

$("#snew").click(function () {  
var container = $("#sblc").add("div");
var content = "<select> <option>Domestic</option><option>Tiger</option><option>Peeled                  Raw</option><option>Cooked Peeled Tail On<option></select><select> <option>1 lbs</option>   <option>2 lbs</option><option>3 lbs</option></select>";

                $(container).attr("class", "opt");
                $(container).append(content); 
                $(container).appendTo("#sblc"); 

            })

下面是对应的HTML:
http://pastie.org/3729304
(我无法弄清楚如何让HTML在这里正常显示。很抱歉的链接。)

Here is the HTML that corresponds: http://pastie.org/3729304 (I couldn't figure out how to get the HTML to display properly on here. Sorry for the link. )

每当我尝试这一点,我最终得到要么全白页,或者什么也不会发生的。
任何帮助将大大AP preciated。

Whenever I try this, I end up either getting a completely white page, or nothing happens at all. Any help would be greatly appreciated.

推荐答案

尝试类似下面,

DEMO

$("#snew").click(function() {
    var container = $("<div />");
    var content = "<select> <option>Domestic</option><option>Tiger</option><option>Peeled                  Raw</option><option>Cooked Peeled Tail On<option></select><select> <option>1 lbs</option>   <option>2 lbs</option><option>3 lbs</option></select>";

    $(container)
        .attr("class", "opt") //add class to the div container
        .append(content)      //append select to the div
        .appendTo("#sblc");   //append div to #sblc

});

这篇关于添加一个字段到表单(jQuery的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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