0

我有一个带脚本的 jquery 聊天

var name = "Guest";
    name = name.replace(/(<([^>]+)>)/ig,"");
    var chat =  new Chat();
    $(function() {
         chat.getState();
         $("#sendie").keydown(function(event) {
             var key = event.which;
             if (key >= 33) {
                 var maxLength = $(this).attr("maxlength");
                 var length = this.value.length;
                 if (length >= maxLength) {
                     event.preventDefault();
                 }
              }
              });
         $('#sendie').keyup(function(e) {
              if (e.keyCode == 13) {
                var text = $(this).val();
                var maxLength = $(this).attr("maxlength");
                var length = text.length;
                if (length <= maxLength + 1) {
                    chat.send("testtext", name);
                    $(this).val("");
                } else {
                    $(this).val(text.substring(0, maxLength));
                }
              }
         });
    });

html结构是;

<body onload="setInterval('chat.update()', 1000)">

    <div id="page-wrap">

        <h2>jQuery/PHP Chat</h2>

        <p id="name-area"></p>

        <div id="chat-wrap"><div id="chat-area"></div></div>

        <form id="send-message-area">
            <p>Your message: </p>
            <textarea id="sendie" maxlength = '100' ></textarea>
            <button id="submit">submit</button>
        </form>

    </div>

</body>

当我在消息后按键时,此发送(或保存到文件)ENTER消息。我已将文本区域数据替换为testtext. 我想在单击submit按钮时发送数据。

我怎样才能做到这一点??

我正在尝试修改示例..如果有人有解决方案,请告诉我..我正在尝试添加一个按钮而不是默认enter按键。

提前致谢...:)

布雷拉弗雷德

4

4 回答 4

1

Use jQuery's ajax() or post(). For example:

$('#submit').click(function() {
    $.ajax({
      url: myUrl,
      cache: false,
      dataType: "json", 
      data: { 'message' : $('#sendie').val() }, 
      success: function(data){
        //...
      },
      error: function(e, xhr){
        //...
      }
    });
});
于 2011-04-10T12:16:25.043 回答
0

您可以在提交表单时尝试触发事件:

$('#sendie').keyup(function(e, extra) {
    if (e.keyCode == 13 || extra.keyCode == 13) {
        var text = $(this).val();
        var maxLength = $(this).attr("maxlength");
        var length = text.length;
        if (length <= maxLength + 1) {
            chat.send("testtext", name);
            $(this).val("");
        } else {
            $(this).val(text.substring(0, maxLength));
        }
    }
 });

...

$("#send-message-area").submit(){
    $('#sendie').trigger('keyup', [{keyCode: 13}]);
    return false;
}

需要该行return false以防止重新加载您的页面。

更新后keyup为名为的函数添加了一个新参数extra,该参数将保存“手动”触发的事件的 keyCode。

于 2011-04-10T12:13:01.117 回答
0

Just take the code from $('#sendie').keyup(), create a function and call it from $('#sendie').keyup() and $('#send-message-area').submit()

something like this

var name = "Guest";
name = name.replace(/(<([^>]+)>)/ig,"");
var chat =  new Chat();
$(function() {
     chat.getState();
     $("#sendie").keydown(function(event) {
         var key = event.which;
         if (key >= 33) {
             var maxLength = $(this).attr("maxlength");
             var length = this.value.length;
             if (length >= maxLength) {
                 event.preventDefault();
             }
          }
          });
     $('#sendie').keyup(function(e) {
          if (e.keyCode == 13) {
             doSend();
             return (false);
          }
     });
     $('#send-message-area').submit(function(e) {
        doSend();
        return (false);
    });
});

function doSend(){
    var text = $('#sendie').val();
    var maxLength = $('#sendie').attr("maxlength");
    var length = text.length;
    if (length <= maxLength + 1) {
        chat.send("testtext", name);
        $('#sendie').val("");
    } else {
        $('#sendie').val(text.substring(0, maxLength));
    }
}
于 2011-04-10T12:15:56.840 回答
0

I solved it by removing chat send from main group and included it in trigger click function as below;

$('#trigger').click(function() {
    chat.send("testtext", name);
    $(this).val("");
    });

Thanks to @Kon, @Piotr Salaciak and @Dutchie432 for their support... :)

于 2011-04-14T04:41:59.003 回答