由于我不能使用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>
解决方法
什么似乎混淆这个线程是之间的区别:
$('.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")
.appendTo("#form-0");
// OR
// see .after() or .before() in the api.jquery.com library
});