jquery appendTo form
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">
< opt开发者_StackOverflow社区ion 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>
What i want to do:
When i press on option value nr 4, there appears is new input field, this thing works fine.
But how can I to change order where input field gonna appear, because now it appears after textfield, how can i put it after ?
Thank you
// 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');
I think you'd want something like this. Note the changed id for the option, and the id added to the select element, to allow positioning of the new input element:
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>
精彩评论