通过jquery ajax将文本区域中的内容加载到div中

问题描述:

I need to load content from a text area into a div via jquery ajax. The content initally loads into a div via jquery. When the button is pressed, a click function inserts the content into a database via ajax and then reloads the content back into the div. It works but seems to be a bit sporadic. My question is, is there a better way to do this.

I am basically trying to submit a status into a timeline without refreshing the page

<script>
    $.ajaxSetup ({ cache: false });     
    var ajax_load = "<div align='center'><img src='img/ajax-loader.gif' alt='loading...' /></div>";
    var loadUrl = "ajax/ajax.php";
    var loadMe = "functions/status.php";
    $("#result").html(ajax_load).load(loadUrl);

    $("#share-but").click(function() {
        var status = $("textarea#status").val(); 
        $.post(
            loadMe,
            { status: status, user_id: '<?php echo "10" ?>' },
            "html"
        );
        $("#result").html(ajax_load).load(loadUrl);
        $("textarea").val('Share an update');
    });
</script>

I am guessing you should be doing your update in the callback function that jquery executes when the server responds to your post request

thus your code should be changed to

<script>
    $.ajaxSetup ({ cache: false });     
    var ajax_load = "<div align='center'><img src='img/ajax-loader.gif' alt='loading...' /></div>";
    var loadUrl = "ajax/ajax.php";
    var loadMe = "functions/status.php";
    $("#result").html(ajax_load).load(loadUrl);

    $("#share-but").click(function() {
        var status = $("#status").val(); 
        $.post(
            loadMe,
            { status: status, user_id: '<?php echo "10" ?>' },
            function(htmlData)
            {
               $("#result").html(htmlData);
               $("textarea").val('Share an update');
            },
            "html"
        );

    });
</script>

Thus here your posts response is html data that is set in the result container