开发者

Javascript/JQuery dispay form input on webpage

开发者 https://www.devze.com 2023-02-09 19:18 出处:网络
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

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;
});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号