Javascript/JQuery dispay form input on webpage
I'm fairly new to Javascript/JQuery. I'm trying to write an app that takes text input开发者_开发技巧 through a form, applies a function to it, and then displays the processed text. I'd rather avoid sending the input to the server, to avoid validation headaches. I can get the text to display in an alert box, but I can't work out how to display it on the webpage itself.
function do_stuff(my_string) {
//...
return my_processed_string;
}
$(':submit').click(function() {
$('#text_input').each(function() {
my_string = $(this).val();
my_processed_string = do_stuff(my_string);
alert(my_processed_string);
});
return false;
});
Any ideas how to modify this so that my_processed_string appears on the webpage instead of in an alert box?
For the purposes of this answer I'm going to assume you want to make my_processed_string appear at the end of a div with an id of "test".
<html>
<head></head>
<body>
<div id="test"></div>
</body>
</html>
Replace the alert line in your example with the following:
$("#test").append(my_processed_string);
Does this produce the result you were looking for?
function do_stuff(my_string) {
return $(selector).append('<input type="text" value="' + my_string + '" />');
}
$(':submit').click(function() {
$('#text_input').each(function() {
my_string = $(this).val();
return do_stuff(my_string);
});
//return false;
});
精彩评论