简体   繁体   English

向表单添加字段 (jQuery)

[英]Adding a field to a form (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.我正在尝试使用 javascript (jquery) 来执行此操作。 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"); 

            })

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. )这是对应的 HTML: http://pastie.org/3729304 (我不知道如何让 HTML 在这里正确显示。抱歉链接。)

Whenever I try this, I end up either getting a completely white page, or nothing happens at all.每当我尝试这个时,我要么得到一个完全白页,要么什么也没有发生。 Any help would be greatly appreciated.任何帮助将不胜感激。

I think you need to do:我认为你需要这样做:

var container = $("<div>"); // notice the `<` and `>`

This way, you create new DIV container and append it later (you've already done this)这样,您创建新的DIV容器,稍后创建append (您已经这样做了)

What about simply calling append once:简单地调用一次 append 怎么样:

    $("#sblc").append("<div class=\"opt\"><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></div>");

You have an error with how your appending.您的追加方式有误。

http://jsfiddle.net/NYnYw/ http://jsfiddle.net/NYnYw/

Try something like below,试试下面这样的东西,

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

});

This will work with the code you provided:这将适用于您提供的代码:

$("#snew").click(function () {
    var container = $("#sblc");
    var content = "<div><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></div>";

    $(container).attr("class", "opt");
    $(container).append(content);
})

Your logic seems weird... You first declare container as $("#sblc").add("div") which refers to a jQuery collection of objects that includes both #sblc and as many div as it can match.您的逻辑似乎很奇怪...您首先将container声明为$("#sblc").add("div") ,它指的是 jQuery 对象集合,其中包括#sblc和尽可能多的div Is this really what you're trying to do?这真的是你想要做的吗?
Then, you double wrap container in jQuery object which is unnecesary.然后,你在 jQuery object 中双层包装container ,这是不必要的。
The part that strikes me as unusual is $(container).appendTo("#sblc") .让我觉得不寻常的部分是$(container).appendTo("#sblc") You're appending the double wrapped container , or #slbc + divs to #slbc ??您要将双包装container#slbc + divs附加到#slbc ?? Don't understand...不明白...
You could try something like this:你可以尝试这样的事情:

$("#snew").click(function () {

    var $container =  # ('#slbc'),
        materials = ['Domestic', 'Tiger', 'Peeled Raw', 'Cooked Peeled Tail On'],
        values = ['1 lbs', '2 lbs', '3 lbs'],
        html = '<select><option>' + materials.join('</option><option>') + '</option></select>' +
            '<select><option>' + values.join('</option><option>') + '</option></select>';

    $container.append(html).addClass('opt');

});

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

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