¿Cómo cambiar correctamente un enlace en javascript a encendido o en vivo?

I am new to php, and I am trying to learn through tutorials. Right now, I am working on a chat tutorial. Lately, I ran into a problem where the tutorial asked that I use bind, but I don't think it works with jquery anymore. I am trying to throw the message to my message box. Here is the code to my javascript

    var chat = {}

    chat.fetchMessages = function () {
$.ajax({ 
    url: 'ajax/chat.php',
    type: 'post',
    data: { method: 'fetch' },
    success: function(data) {
        $('.chat .messages').html(data);
        }
    });
    }

    chat.throwMessage = function (message) {
if ($.trim(message).length != 0) {
        $.ajax({ 
        url: 'ajax/chat.php',
        type: 'post',
        data: { method: 'throw', messsage: message },
        success: function(data) {
            chat.fetchMessages();
            chat.entry.val('');
        }
    });

}

    }

    chat.entry = $('.chat .entry');
    chat.entry.bind('keydown', function(e) {
if (e.keyCode === 13 && e.shiftKey === false) {
    chat.throwMessage($(this).val());
    e.preventDefault();
}
    });


    chat.interval = setInterval(chat.fetchMessages, 4000);
    chat.fetchMessages();

Here is the php file that is being sent the information:

    <?php
    require '../core/init.php';
    if (isset($_POST['method']) === true && empty($_POST['method']) === false) {

$chat       = new Chat();
$method     = trim($_POST['method']);

if ($method === 'fetch') {

    $messages = $chat->fetchMessages();

    if (empty($messages) === true) {
        echo 'There are currently no messages in the chat';
    } else {

        foreach($messages as $message) {
        ?>

                <div class="message">
                    <a href="#"><?php echo $message['username']; ?></a> says:
                    <p><?php echo $message['message']; ?></p>
                </div>

        <?php
        }

    }


 } else if ($method === 'throw' && isset($_POST['message']) === true) {
    $message = trim($_POST['message']);
    if (empty($message) === false) {
        $chat->throwMessage($_SESSION['user'], $message);
    }

}

    }

When I open up inspector, all the tests are working properly, but the php code does not seem to be putting the data on on my message box.

preguntado el 10 de septiembre de 13 a las 00:09

2 Respuestas

$(document).on('keydown', chat.entry, function(e) {

    if (e.keyCode === 13 && !e.shiftKey) {
        e.preventDefault();
        chat.throwMessage( this.value );
    }

});

en lugar de document use rather an #element

Respondido el 10 de Septiembre de 13 a las 00:09

@adeneo LOL yes I did :) copy/paste mistake. Fixed. Thx - Roko C. Bulján

With "bind" only the existing element will be affected, if you add another element this one will not be affected by the event.

Live affects existing elements and elements that will be added in runtime.

Respondido el 10 de Septiembre de 13 a las 01:09

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.