简体   繁体   English

粘贴事件jQuery的AJAX调用有问题

[英]Having an issue with AJAX call on Paste event jQuery

I am trying to get the pasted contents of a form text field to trigger an AJAX action which connects to a php script to deal with the data and then sends the relevant response. 我正在尝试获取表单文本字段的粘贴内容,以触发AJAX操作,该操作将连接到php脚本以处理数据,然后发送相关的响应。

The php script is working perfectly so I know the problem isn't there. PHP脚本运行正常,因此我知道问题不存在。

The jQuery function also works perfectly, by alerting whatever is pasted into the text field if I do this jQuery函数也可以完美运行,它会警告我粘贴到文本字段中的所有内容

$(document).ready(function(){

    $('input').on('paste', function () {

    var capture = this;

      setTimeout(function () {

          var url = $(capture).val();

          alert (url);

      }, 100);  

   });  
});

But when I try to add the AJAX business it all fails. 但是,当我尝试添加AJAX业务时,一切都会失败。

I'm obviously doing it wrong but I don't know the correct way to do it. 我显然做错了,但是我不知道正确的方法。 The way I'm calling AJAX is the same method I use in a whole bunch of other scripts and it works fine in those so it must be to do with where it's being called. 我调用AJAX的方式与我在其他许多脚本中使用的方式相同,并且在那些脚本中效果很好,因此必须与调用位置有关。

I've tried (left off the doc ready to save clutter) : 我已经尝试过(离开文档准备保存混乱状态):

$('input').on('paste', function () {

var capture = this;

    setTimeout(function () {

        var url = $(capture).val();

    }, 100);

        var data = {
                    'action': 'myAction',
                    'security': $( '#my-nonce' ).val(),
                    'url' : url,
                    'check' : check
                   };

        $.post(myajax.ajaxurl, data, function(response) {

                    alert(response);

        }           
});

And also tried : 并且还尝试了:

$('input').on('paste', function () {

var capture = this;

    setTimeout(function () {

        var url = $(capture).val();

        var data = {
                    'action': 'myAction',
                    'security': $( '#my-nonce' ).val(),
                    'url' : url,
                    'check' : check
                   };

        $.post(myajax.ajaxurl, data, function(response) {

               alert(response);

        } 

    }, 100);

});

And I've also tried setting the url var directly in the data var : 而且我还尝试了直接在数据var中设置url var:

var data = {
            'action': 'myAction',
            'security': $( '#my-nonce' ).val(),
            'url' : $(capture).val(),
            'check' : check
           };

But nothing is working. 但是没有任何效果。

I suspect this is to do with the setTimeout function. 我怀疑这与setTimeout函数有关。 Is there a specific way of firing an AJAX call when also using setTimeout? 同时使用setTimeout时,是否有触发AJAX调用的特定方法?

Just for clarity. 为了清楚起见。 I'm trying to trigger an AJAX call on paste event. 我正在尝试在粘贴事件上触发AJAX调用。

try to increase the time, I mean setTimeout(function(){... ,1000} try experimenting with different times. 尝试增加时间,我的意思是setTimeout(function(){...,1000}尝试尝试不同的时间。

If you get the answer then your post call is eating up the time. 如果您得到答案,那么您的求职电话就是在浪费时间。

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

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