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)


- 400或500级别的HTTP响应 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- 失败的 Canvas 360 jquery 插件 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01