由于我不能在表单中使用div,我想知道如何在表单中间添加新字段(并且我不能在这里使用.append() ),而不重新加载页面或重写表单?(使用jQuery)
编辑:这是HTML:
<form id="form-0" name="0">
<b>what is bla?</b><br>
<input type="radio" name="answerN" value="0"> aaa <br>
<input type="radio" name="answerN" value="1"> bbb <br>
<input type="radio" name="answerN" value="2"> ccc <br>
<input type="radio" name="answerN" value="3"> ddd <br>
//This is where I want to dynamically add the new radio or text line
<input type="submit" value="Submit your answer">
//but HERE is where .append() will put it!
</form>发布于 2011-05-24 01:17:46
让这个线程感到困惑的是以下两者之间的区别:
$('.selector').append("<input type='text'/>"); 它将目标元素附加为.selector的子元素。
和
$("<input type='text' />").appendTo('.selector');它将目标元素附加为.selector的子元素。
注意,当使用不同的方法时,目标元素和.selector的位置是如何变化的。
你想要做的是:
$(function() {
// append input control at start of form
$("<input type='text' value='' />")
.attr("id", "myfieldid")
.attr("name", "myfieldid")
.prependTo("#form-0");
// OR
// append input control at end of form
$("<input type='text' value='' />")
.attr("id", "myfieldid")
.attr("name", "myfieldid")
.appendTo("#form-0");
// OR
// see .after() or .before() in the api.jquery.com library
});发布于 2011-05-23 23:39:18
这将在id为"password“的输入字段后插入一个新元素。
$(document).ready(function(){
var newInput = $("<input name='new_field' type='text'>");
$('input#password').after(newInput);
});不确定这是否回答了您的问题。
发布于 2011-05-23 23:24:50
您可以使用append和appendTo等方法添加任何类型的超文本标记语言:
jQuery manipulation methods
示例:
$('form#someform').append('<input type="text" name="something" id="something" />');https://stackoverflow.com/questions/6099301
复制相似问题