向表单添加字段 (jQuery) [英] Adding a field to a form (jQuery)
问题描述
我正在构建一个表单.没有问题.除了提交按钮之外,我还有另一个按钮,单击该按钮时会向表单添加另一个字段.我正在尝试使用 javascript (jquery) 来做到这一点.不用说,我没有任何运气.我尝试了很多不同的方法,这是我放弃的一种:
$("#snew").click(function () {var container = $("#sblc").add("div");var content = "<select><option>国内</option><option>Tiger</option><option>去皮生</option><option>Cooked Peeled Tail On<option></选择><选择><选项>1磅</选项><选项>2磅</选项><选项>3磅</选项></选择>";$(container).attr("class", "opt");$(容器).追加(内容);$(container).appendTo("#sblc");})
这是对应的HTML:http://pastie.org/3729304(我不知道如何让 HTML 在这里正确显示.抱歉链接.)
每当我尝试这个时,我要么得到一个完全白页,要么什么也没有发生.任何帮助将不胜感激.
尝试如下操作,
$("#snew").click(function() {var container = $("");var content = "<select><option>国内</option><option>Tiger</option><option>去皮生</option><option>Cooked Peeled Tail On<option></选择><选择><选项>1磅</选项><选项>2磅</选项><选项>3磅</选项></选择>";$(容器).attr("class", "opt")//添加类到div容器.append(content)//将选择附加到div.appendTo("#sblc");//将div附加到#sblc});
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");
})
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. )
Whenever I try this, I end up either getting a completely white page, or nothing happens at all. Any help would be greatly appreciated.
Try something like below,
$("#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屋!