开发者

Get textbox contents from AJAX'd DIV

开发者 https://www.devze.com 2023-02-17 16:02 出处:网络
I have a function that displays comments in DIV box from a database. The DIV also has a form for adding new comments.

I have a function that displays comments in DIV box from a database. The DIV also has a form for adding new comments.

The form is got from the PHP script so it is live().

The submit button submits a live click event no probs, but the new comment text is empty.

How can I get开发者_C百科 textfield contents from an AJAX DIV form?

Here is the code:

$('#submit_comment').live('click', function(e) {

        alert('comment submit');
        e.preventDefault();


        var comment = $('#new_comment').live().val(); // ????
        alert(comment); // comment is empty

        if (comment != '') {

            $('#loading').show();
            $('#commentsPanel').hide();
            // loading = true

            var track = $('#trackID').val();

            alert(track);
            var data = 'track=' + track + '&isComment=true&comment=' + comment;
            alert(data);
            $.ajax({
                url: 'comment.php',
                type: 'GET',
                data: data,
                cache: false,
                success: function (comments_html) {
                    alert('submit_comment');
                    $('#commentsPanel').html(comments_html);
                    $('#commentsPanel').show();
                    $('#loading').hide();
                }
            });
        } 
        else { 

        }

});

Many thanks!


Have u tried?

var comment = $('#new_comment').val();

live is used to attach a handler to the event for all elements which match the current selector, now and in the future.

if you are trying to get the value of the textbox only the above code is enough.

0

精彩评论

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