如果在jquery中发生错误,如何在div中显示错误?

时间:2013-09-07 20:06:32

标签: jquery

我的PHP代码是这样的;

if ($mybb->input['action'] == 'addcomment')
{
    if (!empty($mybb->user['kicked_by']))
    {
        echo 'You do not have permissions to chat.';
        exit;
    }

    $query = $db->query("
        SELECT message FROM ".TABLE_PREFIX."chatbox
        WHERE `message` = '{$mybb->input['message']}' AND uid='{$mybb->user['uid']}'
    ");
    $check = $db->fetch_field($query, "message");
    if ($check)
    {
        echo 'The same message has been posted previously.';
        exit;
    }

    if ($mybb->input['private'] == "1")
    {
        $private = '1';
    }
    else
    {
        $private = '0';
    }

    $cuid = intval($mybb->user['uid']);
    $message = $db->escape_string($mybb->input['message']);
    $insert_array = array(
        "uid" => $cuid,
        "message" => $message,
        "edited" => '',
        "approved" => '1',
        "private" => intval($private),
        "dateline" => TIME_NOW,
        "ip" => get_ip()
    );
    $db->insert_query("chatbox", $insert_array);
}
我使用的

和jquery是;

$(".textbox2").live('keypress', function (e) {

if (e.keyCode == 13 && !e.shiftKey) {
    e.preventDefault();

                          var message=$("#message").val();
if (message == '')
{
alert("Message is missing!!");
return;
}

if ($("#private").is(':checked') ? 1 : 0)
{
   var private=$("#private").val();
}

                          $.ajax({
                              type:"post",
                              url:"chat_insert.php",
                              data:"message="+message+"&private="+private+"&action=addcomment",
                              success:function(data){
                                showComment();
                                  document.getElementById('message').value='';
document.getElementById('message').focus();
document.getElementById('private').checked='';
                              },
                        onComplete:function(data){
                             alert("Error: "+data);
                        }

                          });
}

                    });

但我不知道我在那里错过了怎样和什么,这就是为什么没有错误信息输出既没有警报也没有尝试在jquery中使用div id。

请帮忙

1 个答案:

答案 0 :(得分:0)

尝试以下操作(更改了几行)。变量“private”是一个保留字,正如@koala_dev所提到的,live()从1.7开始被弃用。另外,onComplete:应该是完整的:

$( ".textbox2" ).on( 'keypress', function (e) {

            if (e.keyCode == 13 && !e.shiftKey)
            {
                e.preventDefault();

                var message = $( "#message" ).val(),
                    privateid = $( "#private" );

                if (message.trim().length < 1) // trim the message
                {
                    alert( "Message is missing!!" );
                    return;
                }

                if (privateid.is( ':checked' ) ? 1 : 0)
                {
                    var privateidVal = privateid.val();
                }

                $.ajax( {
                            type     : "post",
                            url      : "chat_insert.php",
                            data     : "message=" + message + "&private=" + privateidVal + "&action=addcomment",
                            success  : function (data) {
                                showComment();
                                document.getElementById( 'message' ).value = '';
                                document.getElementById( 'message' ).focus();
                                document.getElementById( 'private' ).checked = '';
                            },
                            complete : function (data) {
                                alert( "Error: " + data );
                            }

                        } );
            }

        } );

以下是Fiddle

上的示例