本文介绍了jQuery on("paste")第一次未获取或传递值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对我来说,是真正的交易问题,因为我不知道如何解决.

Real deal problem for me, because I have no clue to resolve it.

jQuery脚本应在粘贴"操作中获取输入值,并通过ajax将其传递给codeigniter控制器.

jQuery script I wrote should grab the input value on "paste" action and pass it by ajax to codeigniter controller.

它实际上可以正常工作,但前提是我必须第二次(及以后)粘贴该值.当我将某些div或其他DOM元素的on("paste")更改为on("click")时,它也可以正常工作.

It actually works fine, but only if I paste the value for the second (and further) time. It works fine too when I change on("paste") to on("click") for some div or other DOM element.

脚本:

<script type="text/javascript">
$("#link").on("paste", function(){
$("#thumbs").empty().html("<p>loading</p>");
var postData = {
  'url' : $("#link").val()
 }; 

 $.ajax({
     type: "POST",
     url: "/thumb/ajax/",
     data: postData , //assign the var here 
     success: function(msg){
    $("#thumbs").html( "Data Saved: " + msg );
     }

});

});

任何帮助将不胜感激

另一个提示.

val()实际上在粘贴操作之前之前取值.如果我在此输入中写了一些内容,则将其删除并粘贴其他内容,它实际上会将我编写的值(而不是我粘贴的值)传递给控制器​​.

val() in this particular script actually takes value before the paste action.If i write something to this input, then remove it and paste something else it would actually pass to the controller the value i wrote, not the one I pasted.

好的,我敢肯定这是jQuery的粘贴问题.

OK, I'm pretty sure that's jQuery problem with paste.

<script type="text/javascript">

    $("#link").on("paste", function(){
        $("#thumbs").empty().html("<p>loading</p>");
        var postData = {'url' : $("#link").val() }; 
        $("#thumbs").html($("#link").val());
    });


</script>

此脚本应将输入的值添加到div#thumbs.它不适用于第一个粘贴操作.第二,它实际上是有效的.

This script should add value from input to div#thumbs. It doesn't for the first paste action. On second it's actually works.

我的朋友给了我一个提示. on("paste")在"paste"动作上以及粘贴实际完成之前就可以正常工作.如果有人给我提示,如何在粘贴后进行超时以获取值,我可以继续进行.

My friend gave me a hint. on("paste") works right on "paste" action and before the paste is actually done. If anyone give me a hint how to make a timeout to grab the value after the paste I can move on.

推荐答案

问题已解决.

就像我之前说过的那样,"on(" paste)"操作是在实际粘贴上进行的.因此,在将值粘贴到输入之前就已经完成了功能.那是因为第一次粘贴会获取并传递输入的默认值(在我的示例中为null).

Like I said before, "on("paste")" action is being done right on the actual paste. So function is being done before the value is pasted to the input. That's because first time paste grabs and pass the default value of the input (null for my example).

解决方案是设置超时时间.正确的代码看起来像那样,并且工作正常.

Solution is to set the timeout. Right code looks like that and works just fine.

$("#link").on("paste", function(){

setTimeout(function() {
    $("#thumbs").html("<p>loading</p>");
    var postData = {
      'url' : $("#link").val()
      };
    $.ajax({
         type: "POST",
         url: "/thumb/ajax/",
         data: postData , //assign the var here 
         success: function(msg){
        $("#thumbs").html( "Data Saved: " + msg );
            $(".thumb").click(function(){
            (this).attr('id');


        });

         }

    });
}, 500);

});

问题已解决,感谢@ 3nigma的支持.

Problem solved, thanks @3nigma for support.

这篇关于jQuery on("paste")第一次未获取或传递值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 17:51