简体   繁体   中英

How can I display value of a div with a button click?

I am currently working with a wmd editor and jQuery-UI tabs. I have created an ajax/js function that will submit (when next button is clicked) the wmd-preview value and then php echo the result in tab 2. The problem is that I am not getting any results displayed. I am not looking for the textarea value but the div #wmd-preview value. How can I display the value of the div wmd-preview through my ajax/function?

JS

<script>
$(function () {
    var $tabs = $('#tabs').tabs({
        disabled: [0, 1],
        select: function () {
            $.ajax({
                type: "POST",
                url: "post_tabs.php",
                data: {
                    "wmd": $("#wmd-preview").val(),
                },
                success: function (result) {
                    $("#tab-2").html(result);
                }
            });
        }
    });

    $(".ui-tabs-panel").each(function (i) {
        var totalSize = $(".ui-tabs-panel").size() - 1;
        if (i != totalSize) {
            next = i + 2;
            $(this).append("<a href='#' class='next-tab mover' rel='" + next + "'>Next Page &#187;</a>");
        }
        if (i != 0) {
            prev = i;
            $(this).append("<a href='#' class='prev-tab mover' rel='" + prev + "'>&#171; Prev Page</a>");
        }
    });

    $('.next-tab').click(function () {
        var currentTab = $('#tabs').tabs('option', 'selected');
        if (
        (
        currentTab == 0 && /*(B)*/
        $.trim($('#wmd-input').val()).length > 0
        )
        ) {
            var tabIndex = $(this).attr("rel");
            $tabs.tabs('enable', tabIndex).tabs('select', tabIndex).tabs("option", "disabled", [0, 1]);
        } else {
            switch (currentTab) {
            case 0:
                alert('Please fill out all the required fields.', 'Alert Dialog');
                break;
            }
        }
        console.log("preventing default");
        return false;
    });

    $('.prev-tab').click(function () {
        var tabIndex = $(this).attr("rel");
        $tabs.tabs('enable', tabIndex).tabs('select', tabIndex).tabs("option", "disabled", [0, 1]);
        return false;
    });

});
</script>

PHP

<?
if (isset($_POST['wmd'])){
    $wmd = $_POST['wmd']; 
    echo ('<div id="text_result"><span class="resultval"><h2>Textarea Echo result:</h2>'.$wmd.'</span></div>');
                       }
?>

HTML

<div id="tab-1" class="ui-tabs-panel ui-tabs-hide">
    <div id="wmd-button-bar"></div>
    <textarea id="wmd-input" name="wmd-input" cols="92" rows="15" tabindex="6"></textarea>
    <div id="wmd-preview"></div>
</div>

<div id="tab-2" class="ui-tabs-panel ui-tabs-hide">

</div>

PHP code should start with <?php , yours start with <? which is incorrect.

When you see PHP code presented as text - it should tell you it is not running, this is why you keep getting output like '.$wmd.''); } ?> '.$wmd.''); } ?> instead of real PHP echo output.

The other comment still stands as well - you should either use $("#wmd-preview").html() or $("#wmd-input").val() but you cannot use val on divs, it does not work.

In this scenario $("#wmd-input").val() is the better choice just because this is the reason input fields exist - to get input.

Let me know if there are more questions I can help with.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM