简体   繁体   English

使用jQuery / Ajax保存数据

[英]Saving data with jQuery/Ajax

I would like to have an alert whenever people start typing in the text box. 每当有人开始在文本框中输入内容时,我都希望收到警报。 It will return "Data Saved: " + my msg that's in the test.php folder. 它将返回test.php文件夹中的“数据已保存:” +我的消息。

$("#textbox").on('change keyup paste', function() {

           $.ajax({
                  method: "POST",
                  url: "test.php",
                  data: { content: $("#textbox").val() }
                })
                  .done(function( msg ) {
                    alert( "Data Saved: " + msg );
                  });
    });

I've tried changing "method" to "type". 我尝试将“方法”更改为“类型”。 Using .serialize(). 使用.serialize()。 Using success: function 使用成功:功能

I'm not sure what the problem is. 我不确定是什么问题。 Thank you! 谢谢!

Edit: Added an error code. 编辑:添加了错误代码。 So far no error messages on console for this. 到目前为止,在控制台上没有错误消息。

             $.ajax({
                  method: "POST",
                  url: "test.php",
                  data: { content: $("#textbox").val() }
                })

              error: function (request, error) {
                alert("An error occurred");
               },

                success: function (response) {
              if (response == 'OK') {
                $("#diary").val()
               } else {
                alert("An error occurred");


                  }

From your comment you are getting the following error: 从您的评论中,您得到以下错误:

$.ajax is not a function at HTMLTextAreaElement. $ .ajax不是HTMLTextAreaElement的函数。 (loggedinpage.php:92) at HTMLTextAreaElement.dispatch (jquery-3.1.1.slim.min.js:3) at HTMLTextAreaElement.q.handle (jquery-3.1.1.slim.min.js:3) (loggedinpage.php:92)在HTMLTextAreaElement.dispatch(jquery-3.1.1.slim.min.js:3)在HTMLTextAreaElement.q.handle(jquery-3.1.1.slim.min.js:3)

The error is telling you that the code cannot find the $.ajax() function anywhere in order to execute it. 该错误告诉您代码无法在任何地方找到$ .ajax()函数来执行它。

At the same time, the message gives away the fact that you are using jQuery Slim. 同时,该消息表明您正在使用jQuery Slim。 This cut-down version of the library does not include the $.ajax() function, among other things. 该库的简化版本不包括$ .ajax()函数。 You need to use the full version of jQuery. 您需要使用完整版的jQu​​ery。

Details in the latest (or any recent) release notes (in the "Slim build" section): https://blog.jquery.com/2017/03/20/jquery-3-2-1-now-available/ 最新(或任何最新)发行说明中的​​详细信息(在“ Slim build”部分中): https : //blog.jquery.com/2017/03/20/jquery-3-2-1-now-available/

As a side note, I'm surprised that you want to show an alert every single time a user types a character into the textbox. 附带说明,我很惊讶您想在用户每次在文本框中输入字符时都显示警报。 I predict that this will quickly get very annoying for users. 我预计这将很快使用户感到烦恼。 Maybe just showing a message inside a div nearby (which doesn't steal the focus and stop them typing) would be better. 也许只是在附近的div内显示一条消息(这样不会窃取焦点并阻止他们键入内容)会更好。 I'd also question whether it's really necessary to save the data to to the server every time a character is entered? 我还要问,是否真的有必要在每次输入字符时将数据保存到服务器? If the user types quite a lot it will trigger off dozens of ajax requests. 如果用户键入很多,它将触发许多ajax请求。 Your inclusion of the "keyup" is the reason for this. 您包含“关键字”是这样做的原因。 Perhaps have a rethink of exactly how you want this to work, and why. 也许需要重新考虑一下您希望它如何工作以及原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM