Adding a field to a form (jQuery)(向表单添加字段(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");
})
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,
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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:向表单添加字段(jQuery)


基础教程推荐
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01
- npm start 错误与 create-react-app 2022-01-01
- 如何添加到目前为止的天数? 2022-01-01
- fetch 是否支持原生多文件上传? 2022-01-01
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
- 检查 HTML5 拖放文件类型 2022-01-01
- 在 contenteditable 中精确拖放 2022-01-01
- Bootstrap 模态出现在背景下 2022-01-01
- Bokeh Div文本对齐 2022-01-01
- Fabric JS绘制具有活动形状的多边形 2022-01-01