jquery
$(function(){
$('#4').click(function() {
$('<input name="if4" type="text" value="other price>"').appendTo('form');
});
});html
<form>
< input name="name" type="text" value="Enter your name" /><br />
< input name="contacts" type="text" value="Contact info" /><br />
< select name="services">
< option value="1">1</option>
< option value="2">2</option>
< option value="3">3</option>
< option id="4" value="Other">4</option>
< /select><br />
< textarea name="description">Description</textarea><br />
< /form>我想做的是:
当我按下选项值nr 4时,出现了一个新的输入字段,这个东西工作得很好。
但是我如何改变输入字段出现的顺序,因为它现在出现在文本字段之后,我怎么把它放在后面呢?
谢谢
发布于 2010-04-14 16:44:45
// Inserts last in any <form>
$('<p>Test</p>').appendTo('form');
// Inserts first in any <form>
$('<p>Test</p>').prependTo('form');
// Inserts right before any <textarea> in any <form>
$('<p>Test</p>').insertBefore('form textarea');
// Inserts right after any <textarea> in any <form>
$('<p>Test</p>').insertAfter('form textarea');发布于 2010-04-14 16:51:18
我想你会想要这样的东西。注意更改后的选项id,以及添加到select元素的id,以允许定位新的input元素:
jQuery:
$(function() {
$('#opt4').click(function() {
$('<input name="if4" type="text" value="other price">').insertAfter('#services');
});
});HTML:
<form>
<input name="name" type="text" value="Enter your name" /><br />
<input name="contacts" type="text" value="Contact info" /><br />
<select id="services" name="services">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option id="opt4" value="Other">4</option>
</select><br />
<textarea name="description">Description</textarea><br />
</form>https://stackoverflow.com/questions/2635937
复制相似问题