0

我有一个 jquery ajax 帖子,当用户在消息框中输入并按 enter 时,这个 ajax 会触发。但是当用户写Nothing并按下回车时,这个ajax被触发并返回一些东西。但是我怎样才能从用户那里捕获空字符串?

cliKeyPressed : function(e) {

        if(typeof e === "undefined" ) e = window.event;
        if(e.keyCode == 13) {
          var previousLine = "";
          if($('#terminal').text()!== ""){
            previousLine = $('#terminal').text();
            previousLine = previousLine.replace("you entered: ", " ");
          }
          var inputData = $(e.currentTarget).val();

          $('#terminal').text(previousLine + "\r\n" + inputData + "\r\n");
                    AjaxPost(inputData);

        }

AjaxPost : function(data) {

      $.ajax({
       type : "POST",
       url : "/api/user",
       datatype : "application/json",
       contentType: " text/plain",
       data : dataAttribute,
       success : function(data) {

       },
       error : function(error) {

       },
4

1 回答 1

3

你已经在做。只需将 AjaxPost 的调用移到if语句中即可:

if(typeof e === "undefined" ) e = window.event;
    if(e.keyCode == 13) {
        var previousLine = "";
        if($('#terminal').text() !== "" && $(e.currentTarget).val() !== ""){
            previousLine = $('#terminal').text();
            previousLine = previousLine.replace("you entered: ", " ");
            var inputData = $(e.currentTarget).val();
            $('#terminal').text(previousLine + "\r\n" + inputData + "\r\n");
            AjaxPost(inputData);
        }
    }
}
于 2013-09-13T07:48:43.837 回答